使用响应对象下载和上传文件 [英] File download and upload using response object

查看:85
本文介绍了使用响应对象下载和上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件上传控件,我需要在两步后更新该文件
并且我已经在第一步中将该文件流保存在缓存对象中
并保存到最后一步
像这样

第一步

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");



在这里,它正在保存该文件,但未以正确的格式打开,如果是pdf文件,则不会显示,如果是word文件,则它会显示一段时间,而有些时间则无法打开.


我还想使用响应对象通过服务器端下载pdf/rtf/doc/docx文件.



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.

推荐答案

缓存Stream的意义何在?只需使用另存为 [ FileBytes [ ^ ]而不是InputStream.
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.


问题:

1. FileUpload1.FileName仅包含用户计算机上文件的名称,并且只是一个字符串.

2.您似乎将流存储在所有用户共有的缓存中.因此,当多个用户使用此文件时,您最终将覆盖内存中的用户文件.

解决方案:

1.上传文件后,立即将文件保存到磁盘上的临时位置.由于每个用户只有一个文件,因此您可以使用会话ID来命名该文件.

2.使用FileUpload1.FileName找出文件的扩展名并进行相应的保存.

3.在随后的步骤中,使用会话ID从临时位置检索该文件,然后将其保存在所需的任何位置.从临时存储中将其删除.

4.在服务器上安排一整夜的脚本,以从临时位置删除任何文件.这是为了清除任何终止于它们之间的会话中的文件.
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.


这篇关于使用响应对象下载和上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆