views:

430

answers:

4

Hi, I have an ASP.NET web application that uses jQuery on client side. On one of the web forms I have a bunch of controls to fill, and an upload control. User can run upload while filling other entries on the form. When user saves the form, all data including file is stored into database . The question is what to do with uploaded file while user fills the form? I have two possible solutions, but don't know which of them is more optimal

  1. Cache uploaded file, and later, while saving, retrieve it and store into database with all other data.
  2. Save file in temporary folder, and then read it.

What is the best solution for this? Thanks in advance.

A: 

How about:

3: Store it in the database but marked as in an incomplete state

This could be a separate table, or the same table with a status column.

Marc Gravell
Cannot change the database. I'm upgrading existing application
Sorantis
A: 
Traveling Tech Guy
That's why caching file has advantages, like setting expiration time.So if user navigates away from the page cached item will expire.
Sorantis
+1  A: 

I think the most appropriate solution will be storing uploaded file in cache. Here is the code

    var fileKey = Guid.NewGuid();
    var fileStream = new Byte[Request.Files[0].ContentLength];
    Request.Files[0].InputStream.Read(fileStream, 0, Request.Files[0].ContentLength);
    Cache[fileKey.ToString()] = fileStream;

The fileKey GUID can be stored in ViewState, or sent as the response to client. Later, when whole form will be saved, cached file can be retrieved and stored into database with other data. The good thing about this method is that if user navigates from the page cached file will expire, thus avoiding resource flooding. We can set expiration time using

Cache.Add(fileKey, fileStream, null, DateTime.UtcNow.AddMinutes(10), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);

function.

Sorantis
A: 

Like other people mentioned - you can simply upload it. You can do it in many ways:

  1. Upload the files in a temporary folder, and when they finally say save or complete - you can move the file from that temp folder to the other folder where you'd usually keep regular files.
  2. One more thing that you can do, whenever the user clicks on upload button for temporary files, is that you can check you temp folder and clear other files which have stayed there for more than 1-2 days/weeks so that way you know for sure you are not wasting space.

Perhaps, you can also have a timer on your website that regularly cleans up this folder. I'd say this is more efficient and you don't have to wait for users to come in and click on upload button to clean up your space.

BrainCracker
I disagree. This approach is really messy.I need a temp folder, I need a cleanup method, I need a timer.This is too much.As I said, caching file byte array will do the job.
Sorantis
Also, I keep file in database, not in local folder.
Sorantis
Sure, it's messy but i recommended it after reading that you dont' want to alter your db schema. Think about the overhead when 10 users are uploading 2mb file each and that way you are saving to your Cache 20mb. It's a good idea if you, perhaps, have a lot of RAM to play with.
BrainCracker