0

I need to make a Asp.net control to upload multiple files that sends the files through Ajax.

This control could be used to store the files on a Folder or in the Database, the persistense is going to be decided by the Classes that will use my control, not the Control itself. So i need to achieve a level of flexibility here.

The uploaded files will be stored my Control Class like a List of Files, until the class using my control execute the "official post" which will consume and then finally release the class.

In a simple example a Form using my control would consume the List generated by all the uploads made through my control and upong Save, the form would read my list and persist it the way is best.

The problem here is the approach:

I can't use a updatePanel because it doesn't allow for fileinputs and (afaik) won't keep the state of purelly javascript generated input=files.

It's not possible to use the WebMethod property to send the data since it requires my Method to be static which makes it impossible to feed a in memory object using this method.

The other option would be to use handmade Ajax, which is not that difficult, but I'm not sure how Asp.net will behave, does it keep states between Ajax Calls? Can I access the same in memory object from different calls? will i have to keep track of viewstates or use sessions? is it viable to use sessions or the viewstate(!) to keep uploaded files?

Reforcing: The problem is NOT Uploading the files with ajax, I'm using blueimp.github.com/jQuery-File-Upload , the problem is keeping track of them in the serverside without persisting.

Example

Let me try to explain a little better:

0) I have a control, this control has a property that is a List of the files - List<files> FilesUploaded;

1) On each ajax upload , I add the coming File into the list FilesUploaded.Add(FileComingFromAjax)

2) My controls goes into a form (any form), and when I save the Form BtSave.Click(), the Form class will need to get the list of files and persist so: they access it like this AnyonesForm.MyControl.FilesUploaded. Notice that the files are in Memory and not in the disk or the database.

My doubt is, Will the files be kept after each request? namelly:

Will the method call through ajax, use the Same List?

When i submit the main form, Will the List be accessible by the form class with the files I just uploaded?

Jonathan DS
  • 2,050
  • 5
  • 25
  • 48

2 Answers2

2

Well Actually , I was thinking asp.net would be able to keep the objects between ajax requests, but this is not the case.

I'm going to use sessions, I'm not afraid to add objects to sessions anymore , ASP.net, adding objects to a session variable

Community
  • 1
  • 1
Jonathan DS
  • 2,050
  • 5
  • 25
  • 48
1

Combine jQuery and AJAX like so:

jQuery Ajax File Upload

How can I upload files asynchronously?

to achieve your result.

Community
  • 1
  • 1
RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
  • the problem is not Ajax uploading the files, but keeping track of them in memory with asp.net, uploading them is quite trivial with http://blueimp.github.com/jQuery-File-Upload/ – Jonathan DS Feb 22 '13 at 18:04
  • 1
    Gotcha, this is where you'd use that update panel, persist whatever you're persisting to a jquery variable, and as per http://stackoverflow.com/questions/11115365/asp-net-passing-values-from-js-jquery-to-code-behind-c-sharp move it over to your code behind and as like in their example, wrap that button or whatever you're using that would trigger the postback in an update panel to eliminate that postback, while still using AJAX to pass the values to the code behind. Make sense? – RandomUs1r Feb 22 '13 at 18:18
  • yeah, makes sense! I think it may go into the answer. Now I only don't know wheter the list of files will be there when i post the client form – Jonathan DS Feb 22 '13 at 18:24
  • If you've eliminated postback with the AJAX updatepanel, you can just return your jQuery variable to the DOM to display your list of files (jQuery/AJAX will handle the POST to your server), is that what you're asking? – RandomUs1r Feb 22 '13 at 18:32