6

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);
abney317
  • 7,760
  • 6
  • 32
  • 56

1 Answers1

7

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

Darren Blaby
  • 304
  • 2
  • 6
  • That seems pretty similar to my 2nd example above. I tried it with what you posted in my 3rd example above but the file upload didn't go through at all when I used AddParameter – abney317 Jul 31 '17 at 15:19
  • Got it working with the last thing I posted above. Had some other code that was messing it up. So you helped me just enough the first time. Thanks! – abney317 Jul 31 '17 at 22:21
  • Excellent news. Not sure what happened to my example code for main in program.cs, so edited the answer and added it in case someone needs the example in the future – Darren Blaby Aug 01 '17 at 21:15
  • I know this was answered a long time ago, but maybe you guys can help; how do you do the same with a file that is >2gb, the `File.ReadAllBytes` will throw an exception. I tried using the `request.AddFile` but I don't know how make an `Action`. Any help would be appreciated – Emmanuel F Mar 02 '21 at 19:27
  • @EmmanuelF I haven’t looked at this in quite a while, and I’m not at my computer to check for sure, but you may be able to use `()=> File.OpenRead(@“c:\Temp\upload.pdf”)` as the Action argument as it returns a FileStream – Darren Blaby Mar 02 '21 at 19:51
  • @DarrenBlaby, I figured out how to correctly set up the `request.AddFile` (thank you for your suggestion, btw) but it still adds the header information. I'm not confident there is another way of doing this that isn't a multipart form. – Emmanuel F Mar 02 '21 at 23:36
  • @EmmanuelF, It would help if you were able to give some more context, with code samples if possible. I'd suggest asking a question, even if it ends up that it's been asked before – Darren Blaby Mar 03 '21 at 21:19