上传文件到服务器需要 IFileForm 对象作为 POST 参数
Uploading file to server requires IFileForm object as POST parameter
首先,我要说我对网络的经验不多。
我需要将文件上传到服务器,Swagger 文档说它需要 POST 和一些参数:
File : object
UserName : string
同样在 Swagger 文档页面上,它提供了模型
IFormFile {
contentType string
contentDisposition string
headers { <*>: [string] }
length integer($int64)
name string
fileName string
}
我正在制作一个 Xamarin.Forms 应用程序(UWP 和 iOS),请注意 IFormFile 和 FormFile 定义在 AspNetCore 命名空间中,我似乎无法访问它。
所以我做了一个模拟 FormFile class
public class FormFile
{
public string contentType { get; set; }
public string contentDisposition { get; set; }
public List<string> headers { get; set; }
public long length { get; set; }
public string name { get; set; }
public string fileName { get; set; }
}
我正在尝试上传:
public async Task UploadFile1(string filePath, string userName, string authToken)
{
var fileInfo = new FileInfo(filePath);
var fileName = fileInfo.Name;
var formFile = new FormFile
{
contentType = "multipart/form-data",
contentDisposition = "form-data",
headers = new List<string>(),
length = fileInfo.Length,
name = "files",
fileName = fileName
};
using (var client = new HttpClient())
{
client.MaxResponseContentBufferSize = 256000;
client.BaseAddress = new Uri("https://my.url/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
client.DefaultRequestHeaders.Add("x-auth-token", authToken);
using (var content = new MultipartFormDataContent("---upload"))
{
content.Add(new FormUrlEncodedContent(new []
{
new KeyValuePair<string, string>("File", JsonConvert.SerializeObject(formFile)),
new KeyValuePair<string, string>("UserName", userName),
}));
content.Headers.ContentType.MediaType = "multipart/form-data";
var fileStream = File.OpenRead(filePath);
content.Add(new StreamContent(fileStream), fileName, fileName);
using (var response = await client.PostAsync("api/datasets", content))
{
string received;
if (response.IsSuccessStatusCode)
{
received = await response.Content.ReadAsStringAsync();
}
else
{
received = response.ToString();
}
}
}
}
}
我得到的回复是
StatusCode: 422, ReasonPhrase: 'Unprocessable Entity', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Server: Kestrel
Date: Wed, 24 Oct 2018 14:27:10 GMT
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Content-Length: 0
}
我完全不知道如何上传到这个服务器。
IFormData 是处理 "normal" 文件上传的方式。所以我想你应该
- 删除您的 FormFile class,不要发送任何 json 编码的内容。
- 执行 "normal" 文件上传 => 发送文件内容本身作为 "File"
像这样
content.Add(new StreamContent(new MemoryStream(image)), "File", "upload.jpg");
发件人:
请注意,我没有花太多时间寻找文件上传示例(应该有足够的可用)。
首先,我要说我对网络的经验不多。
我需要将文件上传到服务器,Swagger 文档说它需要 POST 和一些参数:
File : object
UserName : string
同样在 Swagger 文档页面上,它提供了模型
IFormFile {
contentType string
contentDisposition string
headers { <*>: [string] }
length integer($int64)
name string
fileName string
}
我正在制作一个 Xamarin.Forms 应用程序(UWP 和 iOS),请注意 IFormFile 和 FormFile 定义在 AspNetCore 命名空间中,我似乎无法访问它。
所以我做了一个模拟 FormFile class
public class FormFile
{
public string contentType { get; set; }
public string contentDisposition { get; set; }
public List<string> headers { get; set; }
public long length { get; set; }
public string name { get; set; }
public string fileName { get; set; }
}
我正在尝试上传:
public async Task UploadFile1(string filePath, string userName, string authToken)
{
var fileInfo = new FileInfo(filePath);
var fileName = fileInfo.Name;
var formFile = new FormFile
{
contentType = "multipart/form-data",
contentDisposition = "form-data",
headers = new List<string>(),
length = fileInfo.Length,
name = "files",
fileName = fileName
};
using (var client = new HttpClient())
{
client.MaxResponseContentBufferSize = 256000;
client.BaseAddress = new Uri("https://my.url/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
client.DefaultRequestHeaders.Add("x-auth-token", authToken);
using (var content = new MultipartFormDataContent("---upload"))
{
content.Add(new FormUrlEncodedContent(new []
{
new KeyValuePair<string, string>("File", JsonConvert.SerializeObject(formFile)),
new KeyValuePair<string, string>("UserName", userName),
}));
content.Headers.ContentType.MediaType = "multipart/form-data";
var fileStream = File.OpenRead(filePath);
content.Add(new StreamContent(fileStream), fileName, fileName);
using (var response = await client.PostAsync("api/datasets", content))
{
string received;
if (response.IsSuccessStatusCode)
{
received = await response.Content.ReadAsStringAsync();
}
else
{
received = response.ToString();
}
}
}
}
}
我得到的回复是
StatusCode: 422, ReasonPhrase: 'Unprocessable Entity', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Server: Kestrel
Date: Wed, 24 Oct 2018 14:27:10 GMT
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Content-Length: 0
}
我完全不知道如何上传到这个服务器。
IFormData 是处理 "normal" 文件上传的方式。所以我想你应该
- 删除您的 FormFile class,不要发送任何 json 编码的内容。
- 执行 "normal" 文件上传 => 发送文件内容本身作为 "File"
像这样
content.Add(new StreamContent(new MemoryStream(image)), "File", "upload.jpg");
发件人:
请注意,我没有花太多时间寻找文件上传示例(应该有足够的可用)。