我目前能夠保存上傳到WebAPI控制器的文件,但我希望能夠將該文件保存爲帶有正確文件擴展名的guid所以它可以被正確地查看。使用guid和文件擴展名ASP.NET WebApi文件上傳
代碼:
[ValidationFilter]
public HttpResponseMessage UploadFile([FromUri]string AdditionalInformation)
{
var task = this.Request.Content.ReadAsStreamAsync();
task.Wait();
using (var requestStream = task.Result)
{
try
{
// how can I get the file extension of the content and append this to the file path below?
using (var fileStream = File.Create(HttpContext.Current.Server.MapPath("~/" + Guid.NewGuid().ToString())))
{
requestStream.CopyTo(fileStream);
}
}
catch (IOException)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.Created;
return response;
}
我似乎無法得到關於內容的實際文件名的手柄。我認爲headers.ContentDisposition.FileName可能是一個候選人,但似乎並沒有填充。
這裏是使用應答類似的問題[http://stackoverflow.com/questions/14937926/file-name-from-httprequestmessage-content][1] [1]: http://stackoverflow.com/questions/14937926/file-name-from-httprequestmessage-content – forseta
你能分享你的請求標題的樣子嗎?您是否在請求中填充了ContentDisposition標頭? –
客戶端負責設置此類標頭。你的客戶是什麼樣的? – Snixtor