Click here to Skip to main content
15,887,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a file upload control, where i need to uplaod that file after 2 step
and i have saved that file stream in cache object in 1st step
and saved it in last step
like this

In 1st step
Cache["resume_stream"] = FileUpload1.PostedFile.InputStream;
Session["resume_name"] = FileUpload1.FileName;



In Last step
resume_name=Session["resume_name"].ToString();                        
StreamWriter sw = new StreamWriter(Server.MapPath("~/resumes/Session["resume_name"].ToString()"));                        
sw.Write((Stream)Cache["resume_stream"]);
sw.Close();
Cache.Remove("resume_stream");


Here it is saving that file but it is not opening in correct format, in case of pdf file it is not showing and in case of word file it is showing some time empty and some time not opening.


and also I want to download file by server side for pdf/rtf/doc/docx file using response object.
Posted
Updated 20-Mar-12 4:53am
v2

What is the point of caching the Stream? Just use the SaveAs[^] method or if you really need to cache it use FileBytes[^] rather than InputStream.
 
Share this answer
 
Issues:

1. FileUpload1.FileName simply contains the name of the file on the user's machine and is just a string.

2. You seem to store the stream in the Cache that is common to ALL users. So you will end up overwriting user's files in memory when multiple users use this.

Solution:

1. Save the File to disk in a temp location straight away after it is uploaded. Since there will be only one file per user, you can use the session id to name the file.

2. Use the FileUpload1.FileName to find out the extension of the file and save accordingly.

3. In later step, retrieve that file from the temp location using the Session ID and save wherever you require. Delete it from temp storage.

4. Schedule a script overnight on the server to delete any files from the temp location. This is to clean up files from any sessions terminating in between.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900