上传 UIImage 类型的图像列表(Xamarin)
Posted
技术标签:
【中文标题】上传 UIImage 类型的图像列表(Xamarin)【英文标题】:Upload a list of images of type UIImage (Xamarin) 【发布时间】:2020-09-01 02:41:04 【问题描述】:我的应用将从特定文件夹中检索所有图像的列表,并尝试通过 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, 0, 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);
【问题讨论】:
现在可以用了吗? 【参考方案1】:我用下面的sn-p存档了上传多个文件到服务器,你可以试试...
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, 0, (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(如果出现问题,出于任何原因),则将其标记为未上传,并在互联网可用时再次尝试上传。
如果您需要更多帮助,可以问...:)
【讨论】:
【参考方案2】:您可以尝试使用MultipartFormDataContent
并将内容类型设置为application/octet-stream
。
你可以参考one和two这两个链接。
【讨论】:
以上是关于上传 UIImage 类型的图像列表(Xamarin)的主要内容,如果未能解决你的问题,请参考以下文章
检查 UIImageView 中的图像不是占位符图像并附加到 UIImage 数组