Problem cleaning up temp folder after saving files to database

A

Author #1

One of my asp.net 3.5 web applications tracks all kinds of user files.
Users can upload files to a storage in addition to other file
information which is stored in the SQL Server 2005 Database.

Currently, my design is to store the files in the same SQL Server 2005
database. This is my preferred method. (I may consider storing files
in the file system, and simply keep the path in the database, if my
current problem cannot get resolved nicely.)

I am using this ajax file upload tool to upload files:
http://en.fileuploadajax.subgurim.net/

This tool uploads the file to a temp folder, and when a user clicks on
a Save button, my application reads the uploaded file from this temp
folder and sends it to the database.

The problem:

1) Because more than 1 user may be uploading files at the same time,
and 1 user during 1 session may upload more than 1 file, the system
must be able to identify *the-just-uploaded* files *by the current
user*. I have been successful in doing this by using a Guid plus
SessionID.

2) Immediately after the files are sent to the database, I attempt to
erase all of them from the temp folder, and this is where I am having
problem.

I think I have to erase any file that has been saved to the database
as a cleanup for the next file upload operation.

The problem is, when my application tries to erase the files
immediately after they are saved to the database, I get an exception
which says: The file is being used by another process.

I guess this is because the deletion attempt happens too soon after
the files are saved to the database.

How would you resolve this problem? I am more interested in the
design. In other words, what technique would you use in this kind of
situation to avoid such file read-then-delete conflict?

I hope that this isn't too long and if it is not clear, please yell.
Thanks.
 
E

Eliyahu Goldin

I am not sure "too soon" is the problem. If you read a file with a stream
and close the stream properly there shouldn't be a problem.


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Author #1 said:
One of my asp.net 3.5 web applications tracks all kinds of user files.
Users can upload files to a storage in addition to other file
information which is stored in the SQL Server 2005 Database.

Currently, my design is to store the files in the same SQL Server 2005
database. This is my preferred method. (I may consider storing files
in the file system, and simply keep the path in the database, if my
current problem cannot get resolved nicely.)

I am using this ajax file upload tool to upload files:
http://en.fileuploadajax.subgurim.net/

This tool uploads the file to a temp folder, and when a user clicks on
a Save button, my application reads the uploaded file from this temp
folder and sends it to the database.

The problem:

1) Because more than 1 user may be uploading files at the same time,
and 1 user during 1 session may upload more than 1 file, the system
must be able to identify *the-just-uploaded* files *by the current
user*. I have been successful in doing this by using a Guid plus
SessionID.

2) Immediately after the files are sent to the database, I attempt to
erase all of them from the temp folder, and this is where I am having
problem.

I think I have to erase any file that has been saved to the database
as a cleanup for the next file upload operation.

The problem is, when my application tries to erase the files
immediately after they are saved to the database, I get an exception
which says: The file is being used by another process.

I guess this is because the deletion attempt happens too soon after
the files are saved to the database.

How would you resolve this problem? I am more interested in the
design. In other words, what technique would you use in this kind of
situation to avoid such file read-then-delete conflict?

I hope that this isn't too long and if it is not clear, please yell.
Thanks.

__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4092 (20090520) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4092 (20090520) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
A

Author #1

I am not sure "too soon" is the problem. If you read a file with a stream
and close the stream properly there shouldn't be a problem.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin




One of my asp.net 3.5 web applications tracks all kinds of user files.
Users can upload files to a storage in addition to other file
information which is stored in the SQL Server 2005 Database.
Currently, my design is to store the files in the same SQL Server 2005
database.  This is my preferred method.  (I may consider storing files
in the file system, and simply keep the path in the database, if my
current problem cannot get resolved nicely.)
I am using this ajax file upload tool to upload files:
http://en.fileuploadajax.subgurim.net/
This tool uploads the file to a temp folder, and when a user clicks on
a Save button, my application reads the uploaded file from this temp
folder and sends it to the database.
The problem:
1) Because more than 1 user may be uploading files at the same time,
and 1 user during 1 session may upload more than 1 file, the system
must be able to identify *the-just-uploaded* files *by the current
user*.  I have been successful in doing this by using a Guid plus
SessionID.
2) Immediately after the files are sent to the database, I attempt to
erase all of them from the temp folder, and this is where I am having
problem.
I think I have to erase any file that has been saved to the database
as a cleanup for the next file upload operation.
The problem is, when my application tries to erase the files
immediately after they are saved to the database, I get an exception
which says: The file is being used by another process.
I guess this is because the deletion attempt happens too soon after
the files are saved to the database.
How would you resolve this problem? I am more interested in the
design. In other words, what technique would you use in this kind of
situation to avoid such file read-then-delete conflict?
I hope that this isn't too long and if it is not clear, please yell.
Thanks.
__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4092 (20090520) __________
The message was checked by ESET NOD32 Antivirus.

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4092 (20090520) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Yes, you are correct. Indeed, I forget to close the FileStream and the
BinaryReader. After I added

fs.Close();
br.Close();

it is all good, now.

Thanks a lot for this reminder. I keep forgetting closing IO streams.
 
A

Author #1

Andrew said:
If you open them with a "using" statement, they will be closed
automatically.

See "Using block" in the help.

Andrew

I have been using the using block for db operations all the time,
haven't used it for file IO, well, I rarely do file IO operation, so,
I haven't got used to it. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top