如何解决上载UIImageXamarin类型的图像列表
我的应用将从特定文件夹中检索所有图像的列表,并尝试通过API端点将其上传到服务器
由于上述要求,图像选择器不适合
下面是共享代码中传递了UIImages列表的方法(我现在试图使其仅与ios一起使用,但最终同样的情况也将适用于Android)
以下内容不起作用,因为当我在服务器(AWS)上查看图像时,它是代码格式。它还说内容类型是服务器上的application / json,我将其设置为image / png时无法理解
private async Task UploadImages(List<UIImage> images)
{
HttpClient client = new HttpClient();
var contentType = new MediaTypeWithQualityHeaderValue("image/png");
client.DefaultRequestHeaders.Accept.Add(contentType);
client.DefaultRequestHeaders.Add("Id-Token",Application.Current.Properties["id_token"].ToString());
foreach (var image in images)
{
try
{
string baseUrl = $"https://********/dev/ferret-test/media/team1/user1/device1/test1.png";
client.BaseAddress = new Uri(baseUrl);
//UploadModel uploadModel = new UploadModel
//{
// image_file = image.AsPNG()
//};
byte[] bArray = null;
Stream pst = image.AsPNG().Asstream();
using (MemoryStream ms = new MemoryStream())
{
ms.Position = 0;
pst.copyTo(ms);
bArray = ms.ToArray();
}
//string stringData = JsonConvert.SerializeObject(bArray);
//var contentData = new StringContent(stringData,//System.Text.Encoding.UTF8,"image/png");
//Byte[] myByteArray = new Byte[imageData.Length];
//System.Runtime.InteropServices.Marshal.copy(imageData.Bytes,myByteArray,Convert.ToInt32(imageData.Length));
var postRequest = new HttpRequestMessage(HttpMethod.Put,baseUrl)
{
Content = new ByteArrayContent(bArray)
};
var response = await client.SendAsync(postRequest);
response.EnsureSuccessstatusCode();
string stringJWT = response.Content.ReadAsstringAsync().Result;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
解决方法
您可以尝试使用MultipartFormDataContent
并将内容类型设置为application/octet-stream
。
我使用以下代码段将多个文件上传到服务器,您可以尝试一下...
foreach (SQLiteAccess.Tables.Image image in images.OrderByDescending(x => x.Id)) //Here is the collection of all the file at once (Documents + Images)
{
int documentId = UploadImageToServerAndroid(image).Result;
// My other code implementation
.
.
.
}
private async Task<int> UploadImageToServerAndroid(SQLiteAccess.Tables.Image image)
{
int documentId = 0;
if (!Admins.ConnectedToNetwork()) return documentId;
MyFile = FileSystem.Current.GetFileFromPathAsync(image.Path).Result;
if (MyFile == null) return documentId;
Stream stream = MyFile.OpenAsync(FileAccess.Read).Result;
byte[] byteArray;
byteArray = new byte[stream.Length];
stream.Read(byteArray,(int)stream.Length);
if( !image.IsDocument )
{
try
{
byteArray = DependencyService.Get<IImageUtilities>().CompressImage(byteArray); //Its custom code to compress the Image.
}
catch (Exception ex)
{
UoW.Logs.LogMessage(new LogDTO { Message = ex.Message,Ex = ex });
}
}
string url = "Your URL";
using (HttpClient client = new HttpClient(new RetryMessageHandler(new HttpClientHandler())))
{
try
{
client.DefaultRequestHeaders.Add(Properties.Resources.Authorization,Sessions.BearerToken);
client.DefaultRequestHeaders.Add("DocumentSummary",image.Comment);
client.DefaultRequestHeaders.Add("DocumentName",Path.GetFileName(image.Path));
MultipartFormDataContent multiPartContent = new MultipartFormDataContent();
ByteArrayContent byteContent = new ByteArrayContent(byteArray);
byteContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
multiPartContent.Add(byteContent,"image",Path.GetFileName(image.Path));
HttpResponseMessage response = await client.PostAsync(url,multiPartContent);
if (response.IsSuccessStatusCode && response.Content != null)
{
string jsonString = response.Content.ReadAsStringAsync().Result;
DocumentDTO result = JsonConvert.DeserializeObject<DocumentDTO>(jsonString);
documentId = result.DocumentId;
}
}
catch(Exception ex)
{
UoW.Logs.LogMessage( new LogDTO { Message = ex.Message,Ex = ex });
return documentId;
}
}
return documentId;
}
如果documentid为0(如果由于某种原因出了问题),则标记为未上传,并在互联网可用时尝试再次上传。
如果您需要更多帮助,可以询问...:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。