Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-28-2012, 08:56 PM   PM User | #1
akvarel
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
akvarel is an unknown quantity at this point
How to validate a file in a javascript

Hello!
I am trying to show the errors that can occur by uploading a file (file extension, size).
I am using an adapted version of Jquery Fiel Upload for Ruby on Rails

I need to validate a file (ending of a file, size of the file) before uploading I want to ask if somebody knows how to do it in a following javascript:

Code:
<script type="text/javascript" charset="utf-8">
              $(function () {
                  // Initialize the jQuery File Upload widget:
                  $('#fileupload').fileupload();
                  // 
                  // Load existing files:
                  $.getJSON($('#fileupload').prop('action'), function (files) {
                    var fu = $('#fileupload').data('fileupload'), 
                      template;
                    fu._adjustMaxNumberOfFiles(-files.length);
                    console.log(files);
                    template = fu._renderDownload(files)
                      .appendTo($('#fileupload .files'));
                    // Force reflow:
                    fu._reflow = fu._transition && template.length &&
                      template[0].offsetWidth;
                    template.addClass('in');
                    $('#loading').remove();
                  });
            
              });
        </script>


I am thinking of something like this (the code downstairs) but I need to rewrite the above code to present validation line
Code:
`types = /(\.|\/)(gif|jpe?g|png)$/i`
and to have a error-message
Code:
`alert("#{file.name} is not a gif, jpeg, or png image file")`
. As I am not god at writing javascripts I am asking for help.


Code:
 jQuery ->
      $('#new_painting').fileupload
        dataType: "script"
        add: (e, data) ->
          types = /(\.|\/)(gif|jpe?g|png)$/i
          file = data.files[0]
          if types.test(file.type) || types.test(file.name)
            data.context = $(tmpl("template-upload", file))
            $('#new_painting').append(data.context)
            data.submit()
          else
            alert("#{file.name} is not a gif, jpeg, or png image file")
        progress: (e, data) ->
          if data.context
            progress = parseInt(data.loaded / data.total * 100, 10)
            data.context.find('.bar').css('width', progress + '%')
Many many thanks in advanced
akvarel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:49 PM.


Advertisement
Log in to turn off these ads.