0

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.

4
  • 4
    Are you really willing to send a complete file to the server just to check its size? Commented Aug 9, 2010 at 8:24
  • yes i can send the complete file but i want to check its size and type before saving Commented Aug 9, 2010 at 8:32
  • 1
    @gustavogb: So whats your kind sugesstion Commented Aug 9, 2010 at 8:39
  • I tend to agree with gustavogb....but if you must, you should probably check that the "form" is serialised correctly, and your file-size-checking page is receiving it correctly. Commented Aug 9, 2010 at 11:20

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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!

2 Comments

form11 have enctype define to "multipart/form-data" but its not working
well, as I said, I don't think $.ajax supports this out of the box. You need a jquery plugin..
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.