Save partial uploaded files on cancel or connection loss

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Save partial uploaded files on cancel or connection loss

Andrei Varga
Hello,

Here's an example to explain my problem..
Let's say I'm uploading 5 files, and after 4 out of 5 files are uploaded (i can see them in the temp dir) the connection to the client/browser is lost (or cancel is pressed).
Is there any way to save those 4 files?

Is there an fileuploaded event triggered after each file is uploaded? Or the only way to do this is to write my own UploadStorageProvider class?
Reply | Threaded
Open this post in threaded view
|

Re: Save partial uploaded files on cancel or connection loss

Dean Brettle
Administrator
I think that from an Application_Error handler you should be able to use UploadModule.Files to access the files that completed uploading before the interruption.  I'm not sure whether the file that was in progress will be that list.

--Dean
Reply | Threaded
Open this post in threaded view
|

Re: Save partial uploaded files on cancel or connection loss

Andrei Varga
Cool, the Application_Error handler works, I can see all the completed files in the UploadedModule.Files list.

However, this is not triggered when cancel is pressed (I want to provide the end-user with the opportunity to keep or delete the uploaded files - perhaps this could be a feature request?)

Also, I can't access the Session from that handler and this complicates the design..

The best thing that would help me is a server-side event after each file was uploaded and I can't find one.
I think this can be accomplished by writing my own UploadStorageProvider class, right?
Reply | Threaded
Open this post in threaded view
|

Re: Save partial uploaded files on cancel or connection loss

Dean Brettle
Administrator
I just reviewed the code and realized that I stopped throwing errors on cancelled requests because those errors get logged in some environments and I was getting too many questions about the log messages.  Sorry for misleading you.  The good news is that the Application_EndRequest should still fire.  To distinguish between a successful and unsuccessful upload, set HttpContext.Current.Items["UploadedSucceeded"]  in your upload page and then check for it in the Application_EndRequest handler.

Alternatively, to be notified when each upload completes you would indeed need to create a custom UploadStorageProvider.  If you just derive from an existing provider (e.g. FilesystemUploadStorageProvider) and override CreateUploadedFile() to invoke your desired event and then call base.CreateUploadedFile(), then your handler should be able to use UploadModule.Files to access all preceding files.

Regardless of which approach you take, if you need access to the session, things get more complicated.  That's because at the time NeatUpload is receiving the files, ASP.NET has not attached to the session yet.  That is intentional.  If NeatUpload's processing was done after ASP.NET had attached to the session, the session would be locked for the entire time that the upload occurred.  To workaround this, you can use NeatUpload's SimpleWebRemoting class which is what the SessionBasedUploadStateStoreProvider uses to maintain upload state in the session while the upload is occuring.  SimpleWebRemoting.MakeRemoteCall() will make a request using the same cookies as the current request.  Use it to call a handler that you write.  In your handler, use SimpleWebRemoting.ProcessRemoteCallRequest() to handle the incoming request.  If you want access to the uploaded files from your handler, then you should pass UploadModule.Files as a parameter in your remote call.  Accessing UploadModule.Files from your handler won't work because the request to your handler isn't actually an upload request.

For an example of using the SimpleWebRemoting, see SessionBasedUploadStateStoreProvider.cs and UploadStateStoreHandler.cs.

Clear as mud? :-)

--Dean



Reply | Threaded
Open this post in threaded view
|

Re: Save partial uploaded files on cancel or connection loss

Andrei Varga
Clear enough, thanks :)

I'll try to do this next week.
I haven't mentioned I'm doing this in a ASP.NET MVC application but it shouldn't be any different.