Fraz Sundal
Fraz Sundal

Reputation: 10448

Problem posting file through jquery in asp.net mvc

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.

Upvotes: 0

Views: 208

Answers (3)

DalSoft
DalSoft

Reputation: 11097

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.

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532435

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.

Upvotes: 2

Yngve B-Nilsen
Yngve B-Nilsen

Reputation: 9676

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!

Upvotes: 1

Related Questions