ASP.NET FileUpload无法访问已关闭的文件

时间:2016-11-16 16:29:01

标签: c# asp.net

我知道在SO上还有其他几个这样的问题,但是我从一系列网站,SO,MSDN等等到目前为止所做的每一个答案都证明对错误没有任何影响。< / p>

我有一个RewriteCond %{ENV:MY_VAR} "123" ,在更改时,我将存储在一个会话变量中(我有其他字段会导致回发,所以需要确保我已经获得了用户上传到的文件)。

我得到的错误是

asp:FileUpload

这是{"Cannot access a closed file."} Data: {System.Collections.ListDictionaryInternal} HResult: -2146232798 HelpLink: null InnerException: null Message: "Cannot access a closed file." ObjectName: "" Source: "mscorlib" StackTrace: " at System.IO.__Error.FileNotOpen()\r\n at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)\r\n at System.Web.HttpRawUploadedContent.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer, Int32 bufferOffset)\r\n at System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer, Int32 bufferOffset, Int32 length)\r\n at System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)\r\n at GCSearchPortal.GlobalFunctions.UploadFile[T](HttpServerUtility server, FileType type, T file, Int32 id) in C:\\Users\\zach.ross-clyne\\Source\\Workspaces\\GC Search Portal\\GCSearchPortal\\GCSearchPortal\\GlobalFunctions.cs:line 241" TargetSite: {Void FileNotOpen()} 部分

中我的web.config中的内容
system.web

导致此问题的代码是:

<httpRuntime useFullyQualifiedRedirectUrl="true" maxRequestLength="1048576" requestLengthDiskThreshold="1073741824"/>

感谢您提前提供任何帮助

1 个答案:

答案 0 :(得分:0)

试试这个:

int fileLength = attFile.ContentLength;

byte[] byteContent = new byte[fileLength];
attFile.InputStream.Read(byteContent, 0, iLength);
using (var memStream = new MemoryStream(byteContent))
{
    System.IO.File.WriteAllBytes(server.MapPath(location + fileName), memStream .ToArray());
}