1/  multipart/form-data方式

using Abp.UI;
using Abp.Web.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http; namespace OwnerSayCar.Web.ApiControllers
{
[RoutePrefix("api/upload")]
public class UploadController : ApiController
{
[DontWrapResult]
[Route("img"), HttpPost]
public async Task ImgFromDataUploadAsync()
{
if (!Request.Content.IsMimeMultipartContent())
throw new UserFriendlyException("上传格式不是multipart/form-data"); string UploadImgType = !string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings.Get("UploadImgType")) ?
ConfigurationManager.AppSettings.Get("UploadImgType") : "jpg,png,gif";
string UploadSaveImgPath = !string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings.Get("UploadSaveImgPath")) ?
ConfigurationManager.AppSettings.Get("UploadSaveImgPath") : "/Resource/Images";
int.TryParse(ConfigurationManager.AppSettings.Get("UploadImgMaxByte"), out int UploadImgMaxByte);
UploadImgMaxByte = UploadImgMaxByte > ? UploadImgMaxByte : ; //创建保存上传文件的物理路径
var root = System.Web.Hosting.HostingEnvironment.MapPath(UploadSaveImgPath); //如果路径不存在,创建路径
if (!Directory.Exists(root)) Directory.CreateDirectory(root); var provider = new MultipartFormDataStreamProvider(root); //读取 MIME 多部分消息中的所有正文部分,并生成一组 HttpContent 实例作为结果
await Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.FileData)
{
//获取上传文件名 这里获取含有双引号'" '
string fileName = file.Headers.ContentDisposition.FileName.Trim('"');
//获取上传文件后缀名
string fileExt = fileName.Substring(fileName.LastIndexOf('.')); FileInfo fileInfo = new FileInfo(file.LocalFileName); if (fileInfo.Length > && fileInfo.Length <= UploadImgMaxByte)
{
if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(UploadImgType.Split(','), fileExt.Substring().ToLower()) == -)
{
fileInfo.Delete();
throw new UserFriendlyException("上传的文件格式不支持");
}
else
{
//string newFileName = fileInfo.Name + fileExt;
string newFileName = Guid.NewGuid().ToString() + fileExt;
//最后保存文件路径
string saveUrl = Path.Combine(root, newFileName);
fileInfo.MoveTo(saveUrl);
}
}
else
{
fileInfo.Delete();
throw new UserFriendlyException("上传文件的大小不符合");
}
}
}
}
}

Error:

Error reading MIME multipart body part

解决方法:

web.config:

<system.web>
<!--设置允许请求的长度,比如设置为1GB-->
<httpRuntime targetFramework="4.5.1" maxRequestLength="" />
</system.web>
05-11 19:59