【发布时间】:2012-07-27 01:00:25
【问题描述】:
我正在使用 asp.net mvc 4 web api 开发某种服务。在一个表单上,用户必须上传几个文件,然后将表单提交到服务器。问题在于 ajax 文件上传到 asp.net mvc web api。我已经实现了没有 ajax 的上传。但我需要用ajax完成。 这是
的实现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);
});
我找到了使用 HTML5 http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/ 执行此操作的文章。我需要在 IE8 中进行这项工作。也许你有什么想法?
任何帮助表示赞赏, 伊莉娜。
【问题讨论】:
标签: jquery asp.net-mvc file-upload asp.net-mvc-4 asp.net-web-api