Attualmente sono in grado di salvare un file da caricare su un controller WebAPI, ma mi piacerebbe poter salvare il file come guida con l'estensione corretta del nome file quindi può essere visualizzato correttamente.Caricamento file ASP.NET WebApi tramite guid ed estensione file
Codice:
[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;
}
io non riesco a ottenere una maniglia sul nome effettivo del contenuto. Ho pensato che headers.ContentDisposition.FileName potrebbe essere un candidato ma non sembra essere popolato.
Ecco una domanda simile con una risposta [http://stackoverflow.com/questions/14937926/file-name-from-httprequestmessage-content][1] [1]: http://stackoverflow.com/questions/14937926/file-name-from-httprequestmessage-content – forseta
Puoi condividere l'aspetto delle intestazioni della tua richiesta? hai l'intestazione ContentDisposition popolata nella richiesta? –
È responsabilità del cliente impostare tali intestazioni. Com'è il tuo cliente? – Snixtor