在IIS 8.5上部署后出现文件上载web api 2.0错误

本文关键字:web 上载 文件 api 错误 IIS 部署 | 更新日期: 2023-09-27 17:54:49

以下代码在VS 2012开发环境中运行良好,图像保存到App_Data。

当我在Win 8.1、IIS 8.5上部署相同的应用程序时,App_Data丢失了。我手动创建文件夹并设置中给定的属性http://hintdesk.com/tutorial-publish-asp-net-web-api-in-iis-8-5-and-windows-8-1/.

我看到了奇怪的行为,因为上传不一致。上传偶尔会失败。不知道为什么。

应用程序托管在进行开发的同一台计算机上,IIS上的端口#设置为同一开发端口#

    [HttpPost]
    [Route("v1/persons/{personId}/image")]
    public IHttpActionResult CreatePersonImage(int personId)
    {
        if (Request.Content.IsMimeMultipartContent())
        {
            var isUpdated = 0;
            Request.Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(new MultipartMemoryStreamProvider()).ContinueWith((task) =>
            {
                MultipartMemoryStreamProvider provider = task.Result;
                foreach (HttpContent content in provider.Contents)
                {
                    Stream stream = content.ReadAsStreamAsync().Result;
                    Image image = Image.FromStream(stream);
                    //var testName = content.Headers.ContentDisposition.Name;
                    var imageFileFullName = Utils.GetImageFileFullName(personId + ".jpg");
                    image.Save(imageFileFullName);
                    PersonDb persondb = new PersonDb();
                    isUpdated = persondb.CreatePersonImage(new Person()
                    {
                        ImageFileName = Path.GetFileName(imageFileFullName),
                        PersonId = personId
                    }, out InternalServerErr);
                }
            });
            return Ok();
        }
        else
        {
            return BadRequest("Error in Content-type. It should be multipart/form-data");
        }
    }

public static string GetImageFileFullName(string imageFileName)
{
    return Path.Combine(HostingEnvironment.MapPath("~/App_Data"), imageFileName);
}

IIS 8.5 上的Web配置

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please     
visit
http://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
<appSettings>
</appSettings>
<system.web>
<compilation targetFramework="4.5">
  <assemblies>
    <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<connectionStrings>
<add name="Conn" connectionString="Server=localhost;Database=sampledb;User Id=sa; Password=test123"></add>
</connectionStrings>
</configuration>

应用程序总是返回200 OK状态,但我在IIS上的App_Data上看不到该文件。

W3C日志

#Fields: date time s-ip cs-method cs-uri7-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status sc-bytes cs-bytes time-taken CutomType CotentLength
2015-06-10 10:27:52 192.168.10.22 POST /v1/persons/4/image - 50737 - 192.168.10.22 RestSharp/105.1.0.0 - 200 0 0 210 6198 95 multipart/form-data;+boundary=-----------------------------28947758029299 5830

文件的大小在几个KB 中

编辑:问题似乎出在async上,所以我跟进了https://stackoverflow.com/a/20356591/2922388同步上传,效果很好

在IIS 8.5上部署后出现文件上载web api 2.0错误

我认为你总是得到200 OK的原因是因为文件是在异步任务中处理的。因此,任务启动,并返回200 OK。由于请求已关闭,处理文件时的任何错误都将丢失。

要捕获错误,可以将任务中的异常记录到数据库中。或者删除任务并以非异步方式保存文件。在这种情况下,我认为你会得到错误,而不是200 OK。

没有编写文件的原因可能与IIS有关,因为它在开发人员机器上工作。其中一个原因可能是文件可能未保存在inetpub中。因此,请在inetpub外部创建一个文件夹,然后重试。

另一个原因可能是IIS权限不足。在这种情况下,请为运行IIS的进程在资源管理器中设置对文件夹的权限。

--编辑--

我重读了你的问题。如果有时失败,则表明IIS可以写入文件夹。

在这种情况下,它可能与异步文件保存有关。也许这条溪流是封闭的或者是不完整的。