0
我已經開發了一個fileupload工具(使用angularjs的nervgh fileupload),可以在開發中正常工作。該文件(S)被髮送到的WebAPI上傳控制器:WebAPI 2文件上傳工作在開發中,在生產中給出404
public class UploadController : ApiController
{
public async Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath(@"~\" + Properties.Settings.Default.uploadDir);
var provider = new MultipartFormDataStreamProvider(root);
var documentNewDir = "";
var documentRelative = "";
var fileName = "";
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
var vergaderDate = Convert.ToDateTime(provider.FormData["vergaderdatum"]);
var jaar = vergaderDate.Year;
var maand= vergaderDate.Month.ToString(CultureInfo.CurrentCulture).PadLeft(2, '0');
var dag =vergaderDate.Day.ToString(CultureInfo.CurrentCulture).PadLeft(2, '0');
//absolute path to documents new dir
documentNewDir = HttpContext.Current.Server.MapPath("/") + Properties.Settings.Default.documentsBaseDir + @"\" + jaar + @"\" + maand + @"\" + dag;
//filename
fileName = file.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
//relativePath and filename
documentRelative = Properties.Settings.Default.documentsBaseDir + @"/" + jaar + @"/" + maand + @"/" + dag + @"/"+ fileName;
if(!Directory.Exists(documentNewDir))
Directory.CreateDirectory(documentNewDir);
File.Move(file.LocalFileName, documentNewDir + @"\"+fileName);
}
var response = Request.CreateResponse(HttpStatusCode.OK, documentRelative);
return response;
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
}
這個工程在開發環境不錯,但部署到MS 2012服務器IIS 8.5生產服務器我在POST API調用得到404 API /上傳。
該應用程序支持一些其他WebAPI調用,在開發/產品環境中工作正常....我如何開始調查/調試這種情況?
下面是提琴手通話(留出了FILEDATA ....):
-----------------------------7df2b09e0350
Content-Disposition: form-data; name="vergaderdatum"
2015-01-30T00:00:00
-----------------------------7df2b09e0350
Content-Disposition: form-data; name="file"; filename="5.0. Vooraankondiging themabijeenkomstSamen naar Buiten, 17 sept.pdf"
Content-Type: application/pdf
%PDF-1.5
%âãÏÓ
和響應
HTTP/1.1 404 Not Found
Content-Type: text/html
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Wed, 14 Jan 2015 15:31:08 GMT
Content-Length: 1245
嘗試使用Fiddler或Postman(用於Chrome)撰寫POST請求併發送到您的Upload/PostFormData操作,並查看您獲得的內容。如果你得到一個404,你的行爲沒有被調用。如果你得到500這是行動的答案。我建議Postmand:它更直觀,可以撰寫一篇Fiddler的文章...除非你在其他場合使用Fiddler – JotaBe
使用提琴手數據編輯電話 –
請自己寫一個請求,不要捕獲文件小提琴手。我知道它不會包含一個文件。希望響應會給我們一個指向下一步行動的指針 – JotaBe