c# - RestSharp AddFile adding multipart form headers to file -
i'm using restsharp's addfile , it's working close fine except files end getting broken due header information that's being added.
-------------------------------28947758029299 content-disposition: form-data; name="user.png"; filename="user.png" content-type: image/png this test image uploaded. if remove these lines file opens fine, otherwise seems corrupt. possible me use addfile without 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 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 (no files went through @ here): edit: 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 default sending files using multi-part form data , zendesk api using (assuming it's 1 https://developer.zendesk.com/rest_api/docs/core/attachments#upload-files) isn't expecting that, writing multi-part boundary identifiers content uploaded file.
this answer https://stackoverflow.com/a/27994251/772973 on other thread should resolve problem.
update
i put console app following code in upload pdf asp.net webapi project created don't have access 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 file, identical original file , content-type header set application/pdf , not multipart/form-data; boundary=-----------------------------28947758029299
update 2
added actual code main in program.cs
Comments
Post a Comment