Flynn
Flynn

Reputation: 6181

asp:FileUpload - Keep track of files to save later

I am working on a form on a page that uses an asp:FileUpload to allow users to upload files to a server. I'm new to ASP and am using C# for my code-behind. The plan is to have the user "attach" files one at a time, adding them to an asp:listbox. Finally when the form is submitted the files in the listbox get saved to the server.

While it seems pretty easy to save files from the FileUpload by using

myFileUpload.SaveAs("path");

I am running into some difficulty figuring out how to keep track of the files independent of the FileUpload. I can get the file names really easily using

Path.GetFileName(myFileUpload.PostedFile.FileName);

but really I need to have some way of keeping track of more than just the names. My first thought was maybe to use a temporary folder of some sort, but the files are going to potentially be pretty large so I don't want to do that because saving might take a while.

How can I keep the file around so that I can save it on the server later independent of the FileUpload?

Upvotes: 1

Views: 2036

Answers (2)

Garrison Neely
Garrison Neely

Reputation: 3289

Once you call SaveAs, you're saving the file. The base FileUpload control won't allow you to cache the file somewhere without actually uploading it to the server first. If you are looking to upload multiple files without uploading until the end, you may need to look into dynamically generating FileUpload controls (as many as the user wants). That way they can select the files to upload one at a time, then hit an "Upload" button at the end.

It's a little clunky to do it that way, though. I'd look for some third-party multiple upload controls. I've used PLUpload in the past.

Upvotes: 1

Darren Wainwright
Darren Wainwright

Reputation: 30727

Rather than using a ListBox I would use actual <asp:FileUpload> controls so you can have access to all of the methods for that control - such as Save Etc.

You can put a bunch of these on your page and simply hide all but the first one. Then have a button to say "Add Another" - then with the click of this button show the next <asp:FileUpload> control - JQuery would be a nice choice to show the next <asp:FileUpload> that is currently hidden.

Then in your postback you can loop through all of your <asp:FileUpload> controls and if it HasFile - which is a property on the control - then perform your saving etc.

Save them into a temporary folder if needed - perhaps renaming the file with a GUID and store this list of GUID's in the users Session so you can grab those when needed.

Upvotes: 1

Related Questions