[英]File upload web api 2.0 error after deployment on IIS 8.5
下面的代碼在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
編輯:問題似乎與異步有關,所以我跟隨https://stackoverflow.com/a/20356591/2922388同步上傳,並且工作正常。
我認為您總能獲得200 OK的原因是因為文件是在異步任務中處理的。 因此,任務開始,並返回200 OK。 處理該文件的任何錯誤都將丟失,因為請求已關閉。
要捕獲錯誤,您可以將任務中的異常記錄到數據庫中。 或刪除任務並保存文件,使其不異步。 在那種情況下,我認為您會得到錯誤而不是200 OK。
由於未在開發人員計算機上工作,因此未寫入文件的原因可能是使用IIS。 原因之一可能是文件未保存在inetpub中。 因此,在inetpub之外創建一個文件夾,然后重試。
另一個原因可能是IIS的權限不足。 在這種情況下,請在資源管理器中的文件夾上為運行IIS的進程設置權限。
-編輯-
我重新閱讀了您的問題。 如果有時失敗,則表明IIS可以寫入該文件夾。
在這種情況下,它可能與異步文件保存有關。 流可能已關閉或不完整。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.