首页 技术 正文
技术 2022年11月11日
0 收藏 363 点赞 4,646 浏览 2235 个字

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>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297