-->

RestSharp AddFile adding multipart form headers to

2020-07-26 15:35发布

问题:

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

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?

Current code:

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

Also tried this with the same result:

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

回答1:

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 https://stackoverflow.com/a/27994251/772973 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();
}

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

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

Added the actual code for Main in program.cs