File upload 如何解决邮递员文件上传错误?
我在我的项目中使用webapi文件上传。我正在和邮递员一起测试。但是,Request.Content.IsMimeMultipartContent()始终返回false 邮递员截图: FileUploadController代码:File upload 如何解决邮递员文件上传错误?,file-upload,asp.net-web-api2,postman,asyncfileupload,File Upload,Asp.net Web Api2,Postman,Asyncfileupload,我在我的项目中使用webapi文件上传。我正在和邮递员一起测试。但是,Request.Content.IsMimeMultipartContent()始终返回false 邮递员截图: FileUploadController代码: public async Task<HttpResponseMessage> UserImageUpload() { try { if (!Request.Content.IsMimeMu
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);
}
}
public异步任务UserImageUpload()
{
尝试
{
如果(!Request.Content.IsMimeMultipartContent())
{
抛出新的HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var userImageUploadPath=HttpContext.Current.Server.MapPath(CommonParameters.UserProfileImageServerPath);
var streamProvider=新的CustomMultipartFormDataStreamProvider(userImageUploadPath);
wait Request.Content.ReadAsMultipartAsync(streamProvider);
var files=新列表();
foreach(streamProvider.FileData中的MultipartFileData文件)
{
Add(Path.GetFileName(file.LocalFileName));
}
return Request.CreateResponse(HttpStatusCode.OK,文件);
}
捕获(异常)
{
ErrorFormat(“UserImageUpload()方法中发生错误-类:FileUploadController-消息:{0}”,异常);
返回请求.CreateResponse(HttpStatusCode.BadRequest);
}
}
这是邮递员的错误。尝试删除内容类型标题。当发送实际的帖子时,浏览器会自动添加正确的标题并创建边界。没有必要在Postman的标题中提及内容类型,我尝试发送没有内容类型的附件。这对我来说很好。
当我使用
Content-Type:multipart/formdata
时,它抛出一个错误,说“无法获得任何响应”。邮递员也使用内容类型发送您的文件附件→文本/纯文本;字符集=utf-8
可能会晚一点。我在ARC中遇到了相同的错误,并通过为文件字段提供一个名称(在第二个屏幕截图上的蓝色复选标记之后)来解决此问题。需要考虑的事情很少:
1) 如果您使用的是邮递员
2) 在正文中为所选文件指定键(PFB屏幕截图供参考)
我试着用fiddler上传文件。它成功了,经历了同样的事情。你找到问题了吗?