简体   繁体   English

为什么在ASP.Net MVC中文件上传失败?

[英]Why is file uploading failing in ASP.Net MVC?

I am uploading files using HttpWebRequest to an ASP.Net MVC application but, for some reason unknown to me, it is failing to upload consistently. 我正在使用HttpWebRequest将文件上传到ASP.Net MVC应用程序,但是由于某种我不知道的原因,它无法一致地上传。

I know the file is good since if you try enough times it does eventually upload and can be viewed on the server just fine. 我知道该文件是好的,因为如果尝试了多次,它最终会上传并可以在服务器上正常查看。 When it fails, neither the server nor client reports any errors directly related to the upload, the upload just stops partway through at a random location and time and my MVC action method is called without the file being loaded ( Request.Files.Count == 0 ). 如果失败,则服务器和客户端都不会报告与上载直接相关的任何错误,上载会在随机的位置和时间中途停止,并且在不加载文件的情况下调用了我的MVC操作方法( Request.Files.Count == 0 )。

This only seems to be a problem in our production environment over DSL. 在我们的DSL生产环境中,这似乎只是一个问题。 The test and development environment works fine and the production environment works fine from in the office (really fast connection to servers) but fails when running it from home over DSL. 测试和开发环境在办公室中工作正常,生产环境在办公室中工作正常(与服务器的真正快速连接),但是当通过DSL在家里运行它时会失败。

As you can see below, the point where it fails is pretty basic. 正如您在下面看到的那样,它失败的点很基本。

[Authorize]
[AcceptVerbs(HttpVerbs.Put | HttpVerbs.Post)]
[ValidateInput(false)]
public int UploadScene(int sceneID, int tourID, string name, int number, PhotoType photoType)
{
    SceneInfo scene;
    if (Request.Files.Count < 1) throw new InvalidOperationException("Image file not uploaded.");
    // process file...
}

It seems that it is probably configuration, but I can't figure what it might be. 看来这可能是配置,但我不知道它可能是什么。 We are running in a cluster (we have 4 web servers) so it might have something to do with that, but I am testing against a single server (I can isolate the machine by name and can verify that it is processing my requests). 我们正在集群中运行(我们有4台Web服务器),因此可能与此有关,但是我正在针对单个服务器进行测试(我可以按名称隔离计算机,并可以验证计算机是否在处理我的请求)。 I have also made sure that it is running in it's own app pool. 我还确保它正在自己的应用程序池中运行。 What else should I check? 我还应该检查什么?

We are using IIS6 and .Net 3.5 on the servers. 我们在服务器上使用IIS6和.Net 3.5。

您是否尝试过将表格包装在适当的<form>标签中?

<% using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { @enctype = "multipart/form-data" })) { %>

I checked out the event viewer and noticed the app pool was recycling due to a virtual memory check. 我签出了事件查看器,发现由于虚拟内存检查,应用程序池正在回收。 I turned that off and was able to upload over 20 images without a problem. 我关闭了该功能,可以顺利上传20张图片。

Of course this doesn't explain why recycling causes the file upload to fail immediately. 当然,这并不能解释为什么回收会导致文件上传立即失败。 I was under the impression that the old pool would continue processing any existing requests until they are complete or the shutdown time limit (we have it setup as 10 minutes in order to handle file uploads). 我的印象是,旧池将继续处理所有现有请求,直到它们完成或关闭时间限制为止(我们将其设置为10分钟以处理文件上载)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM