views:

55

answers:

3

I want to check file size and file type when someone choose a file from file upload so for that i use

$('#fuEnglish1').live('change', function () { 
var form = $("#form11").serialize();
var myurl = '<%= Url.Action("CheckFileSizeandType", "Media") %>';
             $.ajax({
                     url: myurl,
                     type: "POST",
                     data: { Media: form },
                     success: function (mydata) {                    
                     }
             });
});

to post the file. But when it post to controller method Request.Files.Count is equal to 0. what im doing wrong.

+1  A: 

Remember that the forms encodingtype needs to be "multipart/form-data" for it to actually post the filecontent. Unfortunately I don't think the $.ajax supports different enctypes, so you have two options:

  1. Make the form synchronous
  2. Use a third-party async upload component like Uploadify (download and documentation here: http://www.uploadify.com/)

Hope that helps!

Yngve B. Nilsen
form11 have enctype define to "multipart/form-data" but its not working
Fraz Sundal
well, as I said, I don't think $.ajax supports this out of the box. You need a jquery plugin..
Yngve B. Nilsen
+2  A: 

Most browsers will not allow you to send a file with an AJAX request. If you want a cross-platform, asynchronous upload you'll need to use a Flash-, Java-, or iframe-based uploader. For a non-Flash uploader, try Ajax Upload or the jQuery Form plugin.

tvanfosson
A: 

Set <httpRuntime maxRequestLength="YourFileSizeHere"/> upload the file normally and use jQuery to poll an MVC Action for error or success.

This won't get around the problem that ASP.NET has to upload the whole file before you can access any of the object's properties. It also won't get around the problem that you can't upload a file via JavaScript for security reasons, the closest you can is targeting the form to a iframe via Javascript, this is what google does.

DalSoft