【发布时间】:2013-08-30 16:54:20
【问题描述】:
我对 MVC 和 .NET 还很陌生,我遇到了我的第一个问题,我已经坚持了 3 天。对于这篇冗长的帖子,我深表歉意。
我正在处理的部分项目要求用户能够选择 XLS 或 XLSX 文件并将其上传,以便应用程序可以导入数据。上传的文件可能会有超过 20,000 行数据。
我有包含 5000、10000 和 20000 行的测试文件。当我在本地机器上运行我的应用程序(使用 Visual Studio 2010)时,所有这些文件都会保存到网络共享并处理得很好。没有错误。但是,当我将应用程序部署到我们的开发服务器时,5 分钟后我收到一个错误,指出 10k 和 20k 文件无法访问,因为它正在被另一个进程使用。
错误消息:进程无法访问文件“已删除路径\TestBook-10k-rows.xlsx 的这一部分”,因为它正被另一个进程使用。
堆栈跟踪:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at System.Web.HttpPostedFile.SaveAs(String filename)
at System.Web.HttpPostedFileWrapper.SaveAs(String filename)
at edtStar.Controllers.TableController.Import(HttpPostedFileBase UploadFile, FormCollection collection) in <removed this part of the path>\TableController.cs:line 392
10000 行文件大小为 304KB,我在 web.config 中设置了如下长度限制:
<httpRuntime
maxRequestLength="4096"
requestLengthDiskThreshold="1024"/>
5分钟左右我找不到什么特别的东西,但是在我开始上传5分钟后它总是返回错误。
我已经在 Chrome 和 IE 上试过了。两者都通过 localhost 工作,都不能通过我们的 dotnet 应用服务器工作。
堆栈跟踪说它在 SaveAs() 方法上崩溃了,但我可以在网络共享位置看到文件并且大小匹配。
保存文件后,我必须读取它并将数据返回到新视图。在将数据返回到视图之前,对数据进行了相当多的处理,这是我希望等待 5 分钟或更长时间的地方。读完文件后,我关闭连接并从网络共享中删除文件。在我收到异常后不久,该文件也被删除。每次开始上传时文件都不存在。
我是目前唯一使用此应用程序的人,因为它仍在开发中。在我进行测试时,我不相信其他人正在访问网络共享上保存的副本。我可以始终如一地重现此问题,但我不知道为什么会发生。
有没有人见过这样的事情或对我有什么建议?我的猜测是我们的应用服务器上有一个设置,但我无法确定它。
谢谢!
编辑:
这是处理文件上传的代码。
// Validate, save, and read the selected file.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Import(HttpPostedFileBase UploadFile, FormCollection collection)
{
// Do some initial file validation - removed
string filePath = Path.Combine(EDTConstants.NETWORK_SHARE_PATH, Path.GetFileName(UploadFile.FileName));
try
{
// Do some validation on the file that was uploaded.
if (UploadFile == null)
{
// return an error here - there was no file selected.
}
if (UploadFile.ContentLength == 0)
{
// return an error here - the file is empty.
}
if (!filePath.ToUpper().EndsWith("XLS") && !filePath.ToUpper().EndsWith("XLSX"))
{
// return an error here - the file extension is not supported.
}
// Things are good so far. Save the file so we can read from it.
UploadFile.SaveAs(filePath);
DataSet fileDS = new DataSet();
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
using (OleDbConnection conn = new OleDbConnection(connString))
{
conn.Open();
using (DataTable dtExcel = conn.GetSchema("Tables"))
{
// ... a bunch of code removed ...
}
conn.Close();
}
CloseFile(filePath);
}
catch (Exception e)
{
// Unexpected error
EDTUtils.LogErrorMessage(EDTConstants.TABLE_IMPORT, User.Identity.Name, e);
ViewData[EDTConstants.SSN_VD_ERROR_MSG] = EDTConstants.EM_UNEXPECTED + System.Environment.NewLine + e.Message;
ViewData[EDTConstants.VIEWDATA_FILE] = UploadFile.FileName;
CloseFile(filePath);
return View(tab);
}
// Closes a file - logs errors but does not throw any exceptions.
private void CloseFile(string filePath)
{
try
{
System.IO.File.Delete(filePath);
}
catch (IOException ioe)
{
EDTUtils.LogErrorMessage(EDTConstants.TABLE_IMPORT, User.Identity.Name, ioe);
}
}
我确实看到记录了相同的错误,一次是在执行 SaveAs() 时,然后又是在执行 Delete 时。奇怪的是,文件确实消失了。这几乎就像应用服务器有另一个线程在此过程中尝试做某事,而这不会发生在 localhost 上。
编辑
更改 executionTimeout 值后,我注意到 Chrome 在 5 分钟标记处说“Uploading 79%...”,然后该文件在网络共享上消失了,然后重新出现,然后一两分钟后,Chrome表示该页面不可用,并带有“错误代码:ERR_INVALID_RESPONSE”。该文件尚未从网络共享中删除。在 IE 中,我被要求在 5 分钟后再次登录应用程序。
【问题讨论】:
-
20000行文件的大小是多少KB?
-
@MauricioGracia,它是 531KB。
-
你能把上传文件的代码贴出来
-
你只是在赶上回来?
-
不,catch 之后还有更多的处理。它所做的只是检查我们为返回视图而构建的表是否有一些行,然后将该表返回到视图。
标签: c# excel asp.net-mvc-4 file-upload file-in-use