views:

10771

answers:

9

I know how to upload with ASP.net's FileUpload control.

What I want to do is use this jQuery Multiple File Upload Plugin to upload multiple files.

Here is exactly what it does when multiple files are selected for upload:

<input type="file class="multi MultiFile" id="MultiFile1_F3" name="file1[]" style="position: absolute; top: -3000px;">

But I cannot figure out how to manipulate these files from asp.net. I have tried using Request.Files as the following link instructs: http://stackoverflow.com/questions/363286/asp-net-upload-of-multiple-files-after-choosing-them-from-jquery

That doesn't work. I think that only works for controls marked with runat="server" at compile time.

Does anyone know how to do this? Maybe something in Request.Form...?

Thanks for your help!

+1  A: 

It's been a bit since I did that kind of thing in .NET, but once you begin cloning form inputs dynamically, I think you have to go out to Request.Form and find the submitted values manually. I wrote up the jQuery code to clone some (non-file) inputs with sequential identifiers here. As long as you have unique identifiers, you can run a loop to see if Request.Form["MultiFile1_F" + counter] exists and go from there.

Tom
The problem with this is that Request.Form["MultiFile1_FN"] returns a string. This sort of thing works great with regular HTML text boxes.
Ronnie Overby
Ah, duh. What's the length of Request.Files when you send x # of files up?
Tom
length of Request.Files is Zero.
Ronnie Overby
+5  A: 

Two things to check:

  • Make sure your form has the enctype="multipart/form-data" attribute set. This is required to enable uploads.
  • Make sure all file inputs have both id and name attributes set. For some reason, if you don't set both, wierd things happen.

Also, runat="server" shouldn't have anything to do with whether Request.Files works or not -- this is more an issue of the browser actually posting the files.

Chris Hynes
I checked both of those things, and that did not help.
Ronnie Overby
This worked for me - my file input element didn't have a name or id - bizarre! Thanks Chris!
matt
+4  A: 

This jQuery plugin was giving every generated input control the exact same name attribute.

For this reason, the files were not posting.

I built my own javascript solution.

I will post a link to the code in a comment.

Edit

I revisited this and found that what I was trying to do wasn't very difficult at all. I got the the jquery multiple file upload plugin to work fine with my aspx form. I don't know why I was having so much trouble before.

1.) Include the jQuery library on the web form:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" />

2.) Reference the multiple file plugin on the web form (Download it here):

<script src="jquery.MultiFile.pack.js" type="text/javascript">

3.) Add a file input on your web form with class="multi":

<input type="file" class="multi" />

4.) Execute some code or call a method like this on form submission:

    void SendMail(string from, string to, string subject, string body, string smtpServer)
    {
        // create mail message
        MailMessage mail = new MailMessage(from, to, subject, body);

        // attach posted files
        for (int i = 0; i < Request.Files.Count; i++)
        {
            HttpPostedFile file = Request.Files[i];
            mail.Attachments.Add(new Attachment(file.InputStream, file.FileName));
        }

        //send email
        new SmtpClient(smtpServer).Send(mail);
    }

This is all that I had to do to attach multiple files to an email sent from an aspx page.

If you want to increase the total size of the files that can be uploaded, add this to your web.config file:

<system.web>
    <httpRuntime executionTimeout="240" maxRequestLength="30720"/>
</system.web>

The executionTimeout is measured in seconds and maxRequestLength is measured in kilobytes. In this example, the request will timeout after 4 minutes and will allow a 30mb request.

Ronnie Overby
A: 

Hi Ronnie,

I know that your source code works well, but I have an other issue at the moment to send the request as POST method, because when you used server control as this works, but the problem that I have is when I send the request by jQuery ($Ajax) and with the Request.Files just has the last file that you was selected, you have any idea ?

Regards!

I don't have any ideas off hand. Maybe you should post a new question.
Ronnie Overby
A: 

Hi,

I uploaded 2 files thru jquery multiple file upload plugin But i get 0 in Request.Files.Count in For loop.

Please help

Aswin
Make sure your form has the enctype="multipart/form-data" attribute set, you freaking hijacker.
Ronnie Overby
A: 

good stackoverflow...

dd
glad you think so.
Ronnie Overby
+2  A: 

I highly recommend Uploadify as a mulitple file uploader. It uses jquery and flash to allow the user to upload multiple files at once through ctrl + clicking on all desired files. It then displays a queue of the files uploading and removes the file from the queue on completion. It also allows you to specify which extension to allow the user to upload as well which prevents you from having to do extension validation.

EDIT:
If you dont want to use flash Ajax Upload works really well too. If users on my site company's site dont have the right version of flash that works best with uploadify, I switch to Ajax Upload. They both work very well for me.

jmein
As far as I know, Flash uploaders suffer from a bug which sends persistent cookies from IE to the upload URL instead of the cookies from the browser. This can break framework features such as, asp.net's forms authentication mechanism.
Ronnie Overby
yeah but there are several workarounds for this bug which have been mentioned in several of the other 'fileuploader' questions on stackoverflow
jmein
flash? ewwwwwww
Jason
+1  A: 

Check out this link : http://ramanisandeep.wordpress.com/2009/10/28/multiple-file-upload-using-jquery/

Hope this helps.

ali62b
A: 

hi, have you found an answer for that im doing the exact same thing and stucked like you?

kemal can
The accepted answer and my supplied answer would be a good place to start. And you shouldn't ask questions by answering someone else's question.
Ronnie Overby