C# 表单上载多个包含数据的图像
我找到了一些关于如何在Xamarin中上传一个或多个图像的教程。然而,我还没有发现如何发送多张图像,每张图像包含一些卫星数据 这是模型在服务器上的外观:C# 表单上载多个包含数据的图像,c#,asp.net,xamarin,asp.net-web-api,xamarin.forms,C#,Asp.net,Xamarin,Asp.net Web Api,Xamarin.forms,我找到了一些关于如何在Xamarin中上传一个或多个图像的教程。然而,我还没有发现如何发送多张图像,每张图像包含一些卫星数据 这是模型在服务器上的外观: public class AppFileDTO { public IFormFile File { get; set; } public string CategoryName { get; set; } public string CategoryDescription { get; set; } public
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端点:
[HttpPost("UploadAppFiles/{id}")]
public async Task<IActionResult> UploadAppFiles(int id, IEnumerable<AppFileDTO> appFileDTOs)
{
...
}
你将如何上传多个
以下是我如何将图像上传到Postman上进行测试的方法,效果非常理想:
一次选择一个图像,并将其与CategoryName、CategoryDescription、末尾的详细信息一起添加到列表中作为列表发送
现在,您可以发布包含多个图像和其他数据的列表一次选择一个图像,并将其与CategoryName、CategoryDescription、末尾的详细信息一起添加到列表中作为列表发送
现在,您可以发布包含多个图像和其他数据的列表我认为您可以使用
MultipartFormDataContent
添加多个图像,并使用添加数据的值
例如:
using (var content = new MultipartFormDataContent())
{
content.Add(CreateFileContent(vm[0]);
content.Add(CreateFileContent(vm[1]);
var response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
}
private StreamContent CreateFileContent(AppFileDTO appFileDTO)
{
var fileContent = new StreamContent(appFileDTO.File.Stream);//your file stream
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"files\"",
FileName = "\"" + appFileDTO.File.FileName + "\"",
}; // the extra quotes are key here
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
fileContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("CategoryName",appFileDTO.CategoryName));
fileContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("CategoryDescription", appFileDTO.CategoryDescription));
fileContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("Detail", appFileDTO.Detail));
return fileContent;
}
和获取内容配置参数参考我认为您可以使用
MultipartFormDataContent
添加多个图像,并使用添加数据的值
例如:
using (var content = new MultipartFormDataContent())
{
content.Add(CreateFileContent(vm[0]);
content.Add(CreateFileContent(vm[1]);
var response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
}
private StreamContent CreateFileContent(AppFileDTO appFileDTO)
{
var fileContent = new StreamContent(appFileDTO.File.Stream);//your file stream
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"files\"",
FileName = "\"" + appFileDTO.File.FileName + "\"",
}; // the extra quotes are key here
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
fileContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("CategoryName",appFileDTO.CategoryName));
fileContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("CategoryDescription", appFileDTO.CategoryDescription));
fileContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("Detail", appFileDTO.Detail));
return fileContent;
}
和获取内容配置参数参考我最后是这样做的:
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);
我最后是这样做的:
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);
无法向MultipartFormDataContents添加列表无法向MultipartFormDataContents添加列表它可以工作吗?可以工作吗?