从会话中获取文件时出现“无法访问已关闭的文件”错误消息

问题描述 投票:8回答:2

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

无法访问已关闭的文件

tr.PostedFile //<== HttpPostedFile; 
byte[] byteArray = null; 
using (var binaryReader = new BinaryReader(tr.PostedFile.InputStream)) 
{ 
    byteArray = binaryReader.ReadBytes(tr.PostedFile.ContentLength); 
}
c# asp.net file-upload httppostedfile
2个回答
20
投票

将其添加到您的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


1
投票

你用过using吗?

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

© www.soinside.com 2019 - 2024. All rights reserved.