I am developing some sort of service using asp.net mvc 4 web api. On one form user must upload few files and then submit form to server. Problem is in ajax file upload to asp.net mvc web api. I have already implemented upload without ajax. But i need it's done with ajax.
This is implementation of
public Task<HttpResponseMessage> PostJob()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
}
string path = HttpContext.Current.Server.MapPath(string.Format("~/Resources/Documents"));
MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(path);
var request = Request.Content.ReadAsMultipartAsync(provider);
var task = request.ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
string fileName = provider.BodyPartFileNames.FirstOrDefault().Value;
string originalName = provider.BodyPartFileNames.FirstOrDefault().Key.TrimStart('"').TrimEnd('"');
string RandomName = provider.BodyPartFileNames.First().Value + Path.GetExtension(originalName);
FileInfo file = new FileInfo(fileName);
file.CopyTo(Path.Combine(path, originalName), true);
file.Delete();
return new HttpResponseMessage(HttpStatusCode.Created);
});
I have found article that does this using HTML5 http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/. I need this work in IE8. Maybe you have any ideas?
Any help is appreciated,
Iryna.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…