RestSharp AddFile 将多部分表单标题添加到文件 [英] RestSharp AddFile adding multipart form headers to file

查看:49
本文介绍了RestSharp AddFile 将多部分表单标题添加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RestSharp 的 AddFile 并且它几乎可以正常工作,除非我的文件由于添加了此标题信息而最终损坏.

I'm using RestSharp's AddFile and it's working close to fine except my files end up getting broken due to this header information that's being added.

-------------------------------28947758029299
Content-Disposition: form-data; name="user.png"; filename="user.png"
Content-Type: image/png

这只是我上传的一张测试图片.如果我从文件中删除这些行,那么它可以正常打开,否则它似乎已损坏.我可以在不添加这些东西的情况下使用 AddFile 吗?

This was just a test image I uploaded. If I remove these lines from the file then it opens fine, otherwise it seems to be corrupt. Is it possible for me to use AddFile without this stuff getting added?

当前代码:

string contentType = MimeMapping.GetMimeMapping("~/uploads/" + filename); //image/png etc
request.AddFile(filename, Server.MapPath("~") + "\\uploads\\" + filename, contentType);
IRestResponse response = client.Execute(request);

也尝试过同样的结果:

request.AddHeader("Content-Type", contentType);
byte[] bytes = File.ReadAllBytes(Server.MapPath("~") + "\\uploads\\" + filename);
request.AddBody(new {myFile = File.ReadAllBytes(Server.MapPath("~") + "\\uploads\\" + filename) });

还有这个(这里根本没有文件通过):这确实有效

Also this (no files went through at all here): this worked actually

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);

推荐答案

RestSharp 默认使用多部分表单数据和您正在使用的 zendesk api 发送文件(假设它是这个 https://developer.zendesk.com/rest_api/docs/core/attachments#upload-files) 没想到,所以它也将内容中的多部分边界标识符写入上传的文件中.

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.

此其他线程上的此答案 https://stackoverflow.com/a/27994251/772973 应该可以解决您的问题.

This answer https://stackoverflow.com/a/27994251/772973 on this other thread should resolve your problem.

更新

我只是将一个控制台应用程序与以下代码放在一起,以将 pdf 上传到我创建的 ASP.NET WebApi 项目,因为我无法访问 ZenDesk API

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

program.cs 中的主程序:

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();
}

ValuesController.cs 中的代码

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);
}

这上传了文件,它与原始文件相同,内容类型标头设置为application/pdf而不是multipart/form-data;边界=----------------------------28947758029299

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

更新 2

在 program.cs 中添加了 Main 的实际代码

Added the actual code for Main in program.cs

这篇关于RestSharp AddFile 将多部分表单标题添加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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