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

时间:2016-02-19 09:06:43

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

我在项目中使用webapi上传文件。我正在和邮递员一起测试。但是, Request.Content.IsMimeMultipartContent()始终返回false。

邮差截图:

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

4 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:2)

在Postman中没有必要提及标题中的Content-Type,我尝试发送没有Content-Type的附件,它对我来说很好。 当我使用Content-Type: multipart/formdata时,它会抛出错误,说“无法获得任何响应”。邮递员也会使用Content-Type →text/plain; charset=utf-8发送您的文件附件。

答案 2 :(得分:1)

要考虑的事情很少:

1)如果您使用的是邮递员

,请不要发送任何内容类型标题

2)在 Body (PFB屏幕截图供您参考)

中为您选择的文件指定密钥

Please check below postman screenshot for your reference.

答案 3 :(得分:0)

可能有点晚了。我在ARC中遇到了同样的错误,并通过提供文件字段的名称(在第二个屏幕截图上的蓝色复选标记之后)解决了此问题

相关问题