我正在使用 RestSharp 的 AddFile 并且它几乎可以正常工作,除非我的文件由于添加了这个标题信息而最终被破坏。
-------------------------------28947758029299
Content-Disposition: form-data; name="user.png"; filename="user.png"
Content-Type: image/png
Run Code Online (Sandbox Code Playgroud)
这只是我上传的一张测试图片。如果我从文件中删除这些行,那么它可以正常打开,否则它似乎已损坏。我可以在不添加这些东西的情况下使用 AddFile 吗?
当前代码:
string contentType = MimeMapping.GetMimeMapping("~/uploads/" + filename); //image/png etc
request.AddFile(filename, Server.MapPath("~") + "\\uploads\\" + filename, contentType);
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
还尝试了相同的结果:
request.AddHeader("Content-Type", contentType);
byte[] bytes = File.ReadAllBytes(Server.MapPath("~") + "\\uploads\\" + filename);
request.AddBody(new {myFile = File.ReadAllBytes(Server.MapPath("~") + "\\uploads\\" + filename) });
Run Code Online (Sandbox Code Playgroud)
还有这个(这里根本没有文件通过):编辑:这实际上有效
string contentType = MimeMapping.GetMimeMapping("~/uploads/" + filename);
byte[] bytes = File.ReadAllBytes(Server.MapPath("~") + "\\uploads\\" + filename);
request.AddHeader("Content-Type", contentType);
request.AddParameter(contentType, bytes, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
RestSharp is by default sending the files using multi-part form data and the zendesk api you are using (assuming it's this one https://developer.zendesk.com/rest_api/docs/core/attachments#upload-files) isn't expecting that, so it is also writing the multi-part boundary identifiers from the content into the uploaded file.
This answer /sf/answers/1959597601/ on this other thread should resolve your problem.
Update
I just put together a console app with the following code in to upload a pdf to an ASP.NET WebApi project that I created as I don't have access to the ZenDesk API
Main in program.cs:
static void Main(string[] args)
{
RestRequest request = new RestRequest("values?fileName=test.pdf", Method.POST);
request.AddParameter("application /pdf", File.ReadAllBytes(@"C:\Temp\upload.pdf"), ParameterType.RequestBody);
var client = new RestClient(new Uri("http://localhost:55108/api"));
var response = client.Execute(request);
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
Code in ValuesController.cs
public async Task Post(string fileName)
{ static void Main(string[] args)
{
RestRequest request = new RestRequest("values?fileName=test.pdf", Method.POST);
request.AddParameter("application/pdf", File.ReadAllBytes(@"C:\Temp\upload.pdf"), ParameterType.RequestBody);
var client = new RestClient(new Uri("http://localhost:55108/api"));
var response = client.Execute(request);
Console.ReadLine();
}
var file = await this.Request.Content.ReadAsByteArrayAsync();
File.WriteAllBytes($"C:\\Uploaded\\{fileName}",file);
}
Run Code Online (Sandbox Code Playgroud)
This uploaded the file, it was identical to the original file and the content-type header was set to application/pdf and not multipart/form-data; boundary=-----------------------------28947758029299
Update 2
在 program.cs 中添加了 Main 的实际代码
| 归档时间: |
|
| 查看次数: |
18664 次 |
| 最近记录: |