加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

文件上传 – 如何解决邮递员中的文件上传错误?

发布时间:2020-12-15 19:42:42 所属栏目:asp.Net 来源:网络整理
导读:我在我的项目中使用webapi上传文件.我正在和邮递员一起测试.但是,Request.Content.IsMimeMultipartContent()始终返回false. 邮差截图: FileUploadController代码: public async TaskHttpResponseMessage UserImageUpload() { try { if (!Request.Content.I
我在我的项目中使用webapi上传文件.我正在和邮递员一起测试.但是,Request.Content.IsMimeMultipartContent()始终返回false.

邮差截图:

FileUploadController代码:

public async Task<HttpResponseMessage> UserImageUpload()
    {
        try
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var userImageUploadPath = HttpContext.Current.Server.MapPath(CommonParameters.UserProfileImageServerPath);
            var streamProvider = new CustomMultipartFormDataStreamProvider(userImageUploadPath);
            await Request.Content.ReadAsMultipartAsync(streamProvider);

            var files = new List<string>();
            foreach (MultipartFileData file in streamProvider.FileData)
            {
                files.Add(Path.GetFileName(file.LocalFileName));
            }

            return Request.CreateResponse(HttpStatusCode.OK,files);
        }
        catch (Exception exception)
        {
            logger.ErrorFormat("An error occured in UserImageUpload() Method - Class:FileUploadController - Message:{0}",exception);
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }

解决方法

这是邮差错误.尝试删除Content-Type标头.发送实际的帖子时,浏览器会自动添加正确的标题并创建边界.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读