• Webapi文件上传


    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 > 0 ? UploadImgMaxByte : 5242880;
    
    
                //创建保存上传文件的物理路径
                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 > 0 && fileInfo.Length <= UploadImgMaxByte)
                    {
                        if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(UploadImgType.Split(','), fileExt.Substring(1).ToLower()) == -1)
                        {
                            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="1073741824" />
    </system.web>
  • 相关阅读:
    window+Apache+php+mysql注意事项
    【转】QRCode二维码的应用心得
    如何创建和配置服务器证书进行SSL Relay
    Citrix 实践中的问题及解决
    web.config中httpRunTime的属性
    [原]IE9 DOM的自定义属性问题
    添加图片到数据库
    Windows server 2008安装企业CA证书服务
    EXTJS Date 的转换格式化字符
    Extjs 4.1 学习笔记(一)(proxy,loader,treestore)
  • 原文地址:https://www.cnblogs.com/eedc/p/9154856.html
Copyright © 2020-2023  润新知