【问题标题】:Getting "Cannot access a closed file" errormessage when getting file from session从会话中获取文件时出现“无法访问已关闭的文件”错误消息
【发布时间】:2013-12-26 09:47:13
【问题描述】:

我有一个 asp.net FileUpload 控件。 我可以成功上传文件以存储在会话中,但是当我试图获取它的输入流时 (我将文件存储在 HttpPosterFile 中)我收到错误

无法访问已关闭的文件

tr.PostedFile //<== HttpPostedFile; 
byte[] byteArray = null; 
using (var binaryReader = new BinaryReader(tr.PostedFile.InputStream)) 
{ 
    byteArray = binaryReader.ReadBytes(tr.PostedFile.ContentLength); 
}

【问题讨论】:

  • 将文件数据的字节数组存储在会话中而不是使用流中的 HttpPostedFile 不是更好吗?

标签: c# asp.net file-upload httppostedfile


【解决方案1】:

你用过using吗?

如果是,请注意在将字符串放入输入流之前不要关闭它。

【讨论】:

  • tr.PostedFile
【解决方案2】:

将此添加到您的 web.config 文件中

<system.web>
  <httpRuntime useFullyQualifiedRedirectUrl="true" maxRequestLength="15360" requestLengthDiskThreshold="15360"/>
</system.web>

http://sanjaysainitech.blogspot.com/2008/12/file-upload-error-can-not-access-closed.html

【讨论】:

  • 在 web.config 中设置 RequestLengthDiskThreshold(设置为任何值,最大为 MaxRequestSize)可以解决问题。显然 ASP.Net 缓冲输入流的第一块,然后将流视为关闭。如果您设置 MaxRequestLength(例如 1536)但让 RequestLengthDiskThreshold 默认设置,则可能会发生这种情况。这显然会绊倒一些内部代码,这使其成为 Microsoft 错误。设置这两个值可以解决问题。
猜你喜欢
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多