ASP.NET FileUpload无法访问已关闭的文件
本文关键字:文件 访问 NET FileUpload ASP | 更新日期: 2023-09-27 18:18:26
我知道在SO上还有其他几个这样的问题,但是到目前为止,我从一系列网站,SO, MSDN等尝试过的每个答案都证明对错误没有影响。
我有一个asp:FileUpload
,在变化,我存储在一个会话变量(我有其他字段,导致post回所以需要确保我有文件的用户上传到某个地方)。
{"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()}
这就是我的网页。
system.web
部分中的配置<httpRuntime useFullyQualifiedRedirectUrl="true" maxRequestLength="1048576" requestLengthDiskThreshold="1073741824"/>
导致这个问题的代码是:
internal static int UploadFile<T>(HttpServerUtility server, FileType type, T file, int id)
{
if (typeof(T) == typeof(HtmlInputFile) || typeof(T) == typeof(FileUpload))
{
string fileName = null;
string location = null;
dynamic fileUpload = file;
if (fileUpload.PostedFile != null && fileUpload.PostedFile.ContentLength > 0)
{
try
{
/* Get a reference to PostedFile object */
HttpPostedFile attFile = fileUpload.PostedFile;
/* Get size of the file */
int attachFileLength = attFile.ContentLength;
/* Make sure the size of the file is > 0 */
if (attachFileLength > 0)
{
if (attachFileLength < 52428800)
{
/* Get extension for file */
string ext = Path.GetExtension(attFile.FileName);
switch (type)
{
case FileType.TermsOfBusiness:
location = "~/Clients/" + id + "/";
fileName = "TOB";
break;
case FileType.TermsOfRole:
location = "~/Jobs/" + id + "/";
fileName = "TOR";
break;
case FileType.CandidateCV:
location = "~/Candidates/" + id + "/";
fileName = "CV";
break;
case FileType.NewCV:
location = "~/CVs/";
fileName = id.ToString();
break;
}
fileName += ext;
/* Save the file on the server */
System.IO.Directory.CreateDirectory(server.MapPath(location));
string filePath = attFile.FileName;
string fileName2 = fileUpload.FileName;
Stream fStream = fileUpload.FileContent;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length); // <<<<< This line causes the crash
File.WriteAllBytes(server.MapPath(location + fileName), contents);
// This is what the code was at the very beginning before all my changes
//attFile.SaveAs(server.MapPath(location + fileName));
return 2;
}
else
{
return 3;
}
}
else
{
return 1;
}
}
catch (Exception ex)
{
return 1; // <<<<< This is where it's crashing and giving the error above
}
}
else
{
return 0;
}
}
else
{
throw new Exception("Passed type is not supported");
}
}
感谢您的帮助
试试这个:
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());
}