Xamarin.Forms上载带有数据的多个图像 [英] Xamarin.Forms Upload Multiple Images with Data

查看:52
本文介绍了Xamarin.Forms上载带有数据的多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些有关如何在Xamarin中上传一个或多个图像的教程.但是,我还没有找到如何发送多幅图像,而每幅图像都包含一些卫星数据.

I have found several tutorials on how to upload an image or multiple images in Xamarin. However, I have not found how to send multiple images with each image containing some satellite data.

这是服务器上模型的外观:

This is how the model looks like on the server:

public class AppFileDTO
{
    public IFormFile File { get; set; }
    public string CategoryName { get; set; }
    public string CategoryDescription { get; set; }
    public string Detail { get; set; }
}

但是控制器需要数据列表.这是Asp.Net Web Api端点:

But the controller needs a list of data. Here is the Asp.Net Web Api endpoint:

 [HttpPost("UploadAppFiles/{id}")]
 public async Task<IActionResult> UploadAppFiles(int id, IEnumerable<AppFileDTO> appFileDTOs)
 {
     ...
 }

如何上传类似的内容?

我在堆栈上发现了一些关于如何上传带有卫星数据的图像的信息:

I found something on stack overflow on how to upload a single image with satellite data:

MultipartFormDataContent multiContent = new MultipartFormDataContent();

// Image 1
HttpContent fileStreamContent = new StreamContent(vm[0].File);
fileStreamContent.Headers.ContentDisposition = new 
System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") 
{
    Name = "File", 
    FileName = vm[0].File.FileName 
};
fileStreamContent.Headers.ContentType = new 
System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
multiContent.Add(fileStreamContent );

// Satellite data, image 1
multiContent.Add(new StringContent(vm[0].CategoryName), "CategoryName");
multiContent.Add(new StringContent(vm[0].CategoryDescription), "CategoryDescription");
multiContent.Add(new StringContent(vm[0].Detail), "Detail");

// Send
var response = await client.PostAsync(url, multiContent);

您如何上传多个?

这是我出于测试目的将图像上传到Postman上的方法,效果很好:

This is how I upload the images on Postman for testing purposes, which works perfectly:

推荐答案

我最终这样做是这样的:

I ended up doing it like this:

MultipartFormDataContent multiContent = new MultipartFormDataContent();

// appFileDTOs[] contains the data that I am going to submit (image and satellite data)
// ------------------------ Object 1 -----------------------------------

// Image 1
HttpContent fileStreamContent1 = new StreamContent(image);
fileStreamContent1.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") 
{
     Name = "appFileDTOs[0].File", 
     FileName = "YourFileName.something" // e.g. image1.jpg
};
fileStreamContent1.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
multiContent.Add(fileStreamContent1);

// Additional data for image 1
multiContent.Add(new StringContent(appFileDTOs[0].CategoryName), "appFileDTOs[0].CategoryName");
multiContent.Add(new StringContent(appFileDTOs[0].CategoryDescription), "appFileDTOs[0].CategoryDescription");
multiContent.Add(new StringContent(appFileDTOs[0].Detail), "appFileDTOs[0].Detail");

// ------------------------ Object 2 -----------------------------------

// Image 2
HttpContent fileStreamContent2 = new StreamContent(image);
fileStreamContent2.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") 
{
     Name = "appFileDTOs[1].File", 
     FileName =  "YourFileName.something" // e.g. image2.jpg
};
fileStreamContent2.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
multiContent.Add(fileStreamContent2);

// Additional data for image 2
multiContent.Add(new StringContent(appFileDTOs[1].CategoryName), "appFileDTOs[1].CategoryName");
multiContent.Add(new StringContent(appFileDTOs[1].CategoryDescription), "appFileDTOs[1].CategoryDescription");
multiContent.Add(new StringContent(appFileDTOs[1].Detail), "appFileDTOs[1].Detail");

// Send (url = url of api)
var response = await client.PostAsync(url, multiContent);

这篇关于Xamarin.Forms上载带有数据的多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆