file-upload - 如何解决 postman 中的文件上传错误?

标签 file-upload asp.net-web-api2 postman asyncfileupload

我在项目中将文件上传与webapi一起使用。我正在与 postman 进行测试。但是, Request.Content.IsMimeMultipartContent()始终返回false。

postman 截图:

enter image description here

enter image description here

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);
        }
    }

最佳答案

这是 postman 的错误。尝试删除Content-Type header 。发送实际的帖子时,浏览器将自动添加适当的标题并创建边界。

关于file-upload - 如何解决 postman 中的文件上传错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35501327/

相关文章:

c# - Web API 2 REST 服务高级数据过滤

rest - 带有 CORS 和 AttributeRouting 的 ASP.NET Web API2 中不允许使用 HTTP PUT

javascript - 类型错误 : todo is not a function

PHP 复制或移动上传的文件 - 没有任何反应,但没有错误

c# - 当自托管 OWIN 服务器关闭时,如何向 Web API 操作发出取消信号?

asp.net - 在ASP.NET中上载的多个文件选择

java - 我无法使用 graphql-java 上传文件

node.js - npm包中的"Unknown visitor type",主角

c# - ASP.NET、C#、IIS、MIME 类型、文件上传条件

c# - 单个 View 上文件类型的多个输入字段