Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I have a single form on a page. There are several text input fields and such. Right now there is also a jQuery file upload control wherein you can select several files. The problem I have right now is that I'm requiring that the user upload the files first (using the jQuery control) and then I save those files in Session state until the regular form posts the rest of the form fields. Then I pull the previously uploaded files from Session and do what I need to do.

So basically to fill out the form requires two separate POST operations back to the server. The files, then the remaining form fields.

I'm thinking there must be a better way to let a user select his/her files yet not post anything until the user submits the main form to post all the other fields. I've read several posts on this site, but I can't find one that addresses this particular issue.

Any suggestions/assistance is greatly appreciated.

share|improve this question

1 Answer

I believe you can do this using Uploadify. There are two options you'd want to look at. First, set auto to false to prevent selected files from immediately being loaded. Second, use the formdata option to send along your other form fields along with the payload.

You'd then call the upload method when the user submits the form, uploading each file in the queue and sending the form data all at once.

Server Side Part:

You'll probably be submitting the form to an ASPX file or ASHX handler. I prefer using an ASHX handler since they're more light-weight. Both will allow you access to the HttpContext or the HttpRequest object. First, you'll need to check context.Request.Files.Count to make sure files were posted:

if (context.Request.Files.Count > 0) // We have files uploaded
{
   var file = context.Request.Files[0]; // First file, but there could be others
   // You can call file.SaveAs() to save the file, or file.InputString to access a stream
}

Obtaining the other form fields should be just as easy:

var formfield = context.Request["MyFormField"]; // Some form field

You can also write results back to the client, such as a JSON encoded description of any resulting errors:

context.Response.Write(response); // Anything you write here gets passed in to onUploadSuccess

I think that should get you started anyway!

share|improve this answer
1  
Just a bump for this answer. I use uploadify to handle large batches of files, and it's a great tool. Really flexible, and asynchronous, so you can work in the background while the files are uploading. It also gives great progress indicators. Definitely recommend. – Mike Varosky May 21 at 12:40
I'm just a little confused about how I work with that server side. I have an Action Method which is where I'm posting to. I don't see how I'm grabbing both the normal form fields (textbox fields and so forth) plus grabbing the file data. I've looked through the samples on Uploadify's page, but I guess I'm just not "getting it." Can someone post a link to an example of some pseudo code of how I handle the files and form data all at the same time? Also, I appreciate the responses on this matter. – nDev May 21 at 16:31
@nDev - I've added some server side info.. Unfortunately, Uploadify doesn't really have many docs on .NET stuff, but everything you need will be somewhere in the HttpRequest object. – Mike Christensen May 21 at 16:43
Ok, excellent. I'll give 'er a go when I get home tonight. Thanks for the assistance. – nDev May 21 at 17:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.