[英]Unsupported media type exception while sending request to the Webapi using post method
我正在嘗試將圖像上傳到服務器,但出現“不受支持的媒體類型異常”。 在這里,我嘗試將圖像字節上傳為 dto,並且我正在使用 postmethod。 這是我正在使用的示例代碼。
public static async Task<T> UploadProfilePic<T>(string apiUrl, FileDto fileDto)
{
try
{
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(ApplicationApiUrls.AppWebUrl);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + ApplicationContext.AccessToken);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
string filename = fileDto.FileName;
MultipartFormDataContent content = new MultipartFormDataContent();
ByteArrayContent imageContent = new ByteArrayContent(fileDto.ImageBytes);
imageContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = filename
};
content.Add(imageContent);
var response = await httpClient.PostAsync(new Uri(ApplicationApiUrls.AppWebUrl + apiUrl), content);
var stringAsync = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
var responseJson = stringAsync;
return JsonConvert.DeserializeObject<T>(responseJson);
}
LoggingManager.Error("Received error response: " + stringAsync);
return default(T);
}
}
catch(Exception ex)
{
return default(T);
}
}
[HttpPost]
[ActionName("UpdateProfilePicFromMobile")]
public ResponseViewModel UpdateProfilePicFromMobile(FileDto fileDto)
{
try
{
int userId = User.Identity.GetUserId<int>();
var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
if (userId > 0 && fileDto != null && file?.ContentLength > 0)
{
var imageManager = new ImageManager();
imageManager.SaveImage(file.InputStream, file.FileName, ImageTypes.ProfilePicture);
return new ResponseViewModel() { success = true, id = pid.ToString(), message = "Image updated successfully." };
}
return new ResponseViewModel() { success = false, id = "", message = "Sorry, Image faile to update." };
}
catch (Exception ex)
{
Console.WriteLine(ex);
return new ResponseViewModel() { success = false, id = "", message = "Sorry, Image faile to update." +"/ " + ex.Message };
}
}
public class FileDto
{
public string FileName {get;set;}
public byte[] ImageBytes { get; set; }
public int UserId { get; set; }
public string ImageFullPath { get; set; }
}
我在服務器中使用 post 方法來保存圖像。 請提出任何想法,我缺少什么。
默認情況下,Web Api 不支持“multipart/form-data”媒體類型(您發送的)。 所以需要直接處理請求體。
我已經更新了你的服務器方法。 現在它可以讀取發送的文件
[HttpPost]
[ActionName("UpdateProfilePicFromMobile")]
public ResponseViewModel UpdateProfilePicFromMobile()
{
try
{
int userId = User.Identity.GetUserId<int>();
var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
if (userId > 0 && file?.ContentLength > 0)
{
var imageManager = new ImageManager();
imageManager.SaveImage(file.InputStream, file.FileName, ImageTypes.ProfilePicture);
return new ResponseViewModel() { success = true, id = pid.ToString(), message = "Image updated successfully." };
}
return new ResponseViewModel() { success = false, id = "", message = "Sorry, Image faile to update." };
}
catch (Exception ex)
{
Console.WriteLine(ex);
return new ResponseViewModel() { success = false, id = "", message = "Sorry, Image faile to update." + "/ " + ex.Message };
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.