Sto seguendo una guida in questo blog su come pubblicare i dati del modulo e un file. Sto cercando di caricare il file in una directory sul server hanno seguito, ma non funziona.caricamento di un file e modulo Dati in Web Api e angularJS
questa è la funzione Post
[HttpPost]
public async Task<HttpResponseMessage> UploadFile(HttpRequestMessage request)
{
String root = HttpContext.Current.Server.MapPath("~/Logo/");
if (!request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var data = await Request.Content.ParseMultipartAsync();
var httpRequest = HttpContext.Current.Request;
if (data.Files.ContainsKey("file"))
{
var file = data.Files["file"].File; // this is getting the file
var fileName = data.Files["file"].Filename; // this is getting the fileName
// System.Web.HttpPostedFile filePost = data.Files["file"];
String a = root + "" + Path.GetFileName(fileName);
string b = a;
var postedFile = fileName; // this is not working
var filePath = HttpContext.Current.Server.MapPath("~/Logo/" + fileName); // can someone help please
postedFile.SaveAs(filePath); // I
}
if (data.Fields.ContainsKey("description"))
{
var description = data.Fields["description"].Value;
}
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("Thank you for uploading the file...")
};
}
come posso salvare il file nella directory sul server?
hai salvato la giornata! Il post Ajax imposta l'intestazione content-type corretta per FormData solo se si imposta Content-Type in modo indefinito! – Yura