class="cnblogs_code" onclick="cnblogs_code_show('c66e40e9-6428-44cb-82f8-79d885469cc3')">1. 注意给form表单加上enctype = "multipart/form-data" 属性,否则会导致Action的参数HttpPostedFileBase 对象接收不到文件。
2. 注意文件大小,IIS中默认上传的文件大小为4MB ,超过这大小的文件需要在修改配置文件。
3. 如果引用了"js/jquery.mobile.min.js",给form表单加上data-ajax="false",否则也收不到上传的文件。
if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = @"C:/"; var provider = new MultipartFormDataStreamProvider(root); try { StringBuilder sb = new StringBuilder(); var task = Request.Content.ReadAsMultipartAsync(provider); task.Wait(); foreach (var file in provider.FileData) { FileInfo fileInfo = new FileInfo(file.LocalFileName); sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", fileInfo.Name, fileInfo.Length)); } return new HttpResponseMessage() { Content = new StringContent(sb.ToString()) }; } catch (System.Exception e) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); }View Code