[英]How to send File Object to Web api using POST call in Xamarin forms.?
我需要從我的xamarin表單應用程序進行POST調用,我需要使用POST調用將File對象直接上傳到API。有什么方法可以實現這一點。
否,無法發送文件對象。 您可以通過在Base64字符串中轉換文件來作為json發送。 這是建議的行之有效的解決方案。 該鏈接包含用於從Base64來回轉換的代碼。
如果您使用Base64或Byte []發送文件對象,則僅允許限制,最大可能為2-4 Mb,但是如果您的圖像大於它不支持的圖像,則不支持。
因此, 解決方案就是流后內容
var file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions
{
PhotoSize = PhotoSize.Full,
CompressionQuality = 100
});
創建MediaFile對象,例如public MediaFile AttachedImage;
並將文件存儲到其中,從而不會丟失內存流。 就像, AttachedImage = file
API上的郵政編碼,
HttpClient httpClient = new HttpClient();
MultipartFormDataContent mt = new MultipartFormDataContent();
AttachedImage.GetStream().Position = 0;
StreamContent imagePart = new StreamContent(AttachedImage.GetStream());
imagePart.Headers.Add("Content-Type", ImageType);
mt.Add(imagePart, String.Format("file"), String.Format("bk.jpeg"));
requestMessage.Content = mt;
var response = await httpClient.PostAsync("Your URL", mt);
if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadAsStringAsync();
var objRootObjectuploadImage = JsonConvert.DeserializeObject<RootObjectuploadImage>(responseString);
if (objRootObjectuploadImage != null)
{
}
else
{
}
}
else
{
Loading(ActIndicator, false);
await DisplayAlert(res.LAlert, "webserver not responding.", res.LOk);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.