1

我想将用户上传的文件存储在会话变量中,以便我可以从我想要的项目中的任何页面使用它。我正在从语句中检索文件:

httpFileCollection uploads=HttpContext.Current.Request.Files;

我如何在会话变量中存储和检索它?

提前感谢

4

2 回答 2

1

Files对象仅限于当前请求;要在请求之间保持这一点,您需要对上传的数据做一些事情 - 要么SaveAs,要么从InputStream.

IMO,出于可扩展性的原因,将其存储在会话中并不是一个好主意 - 例如,将文件写入磁盘上的共享暂存区并将路径存储在会话中会更自然。

于 2009-04-28T07:27:31.060 回答
1

与您在 Session 状态中存储和检索任何其他对象的方式相同:

//Store
Session["UploadedFiles"] = uploads;

//Retrieve
if (Session["UploadedFiles"] != null)
{
  //try-catch blocks omitted for brevity. Please implement yourself.
  HttpFileCollection myUploads = (HttpFileCollection)Session["UploadedFiles"];

  // Do something with the HttpFileCollection (such as Save).

  // Remove the object from Session after you have retrieved it.
  Session.Remove("UploadedFiles");
}

将此对象存储在 Session 中是否明智值得商榷,但我不推荐它。

至于 HttpFileCollection 变量不能存储在 Session 状态中的争论(参考您之前的问题),我反驳说,因为我过去能够做到这一点。将对象检索到变量中后,您可以根据需要保存它。

于 2009-04-28T07:30:03.363 回答