c# - Any one knows how to upload file from ASP.net to remote rest service ( not WCF) -


i need implement upload method in asp.net c# upload file remote rest service instead uploading local machine.

i have wrote function posts data rest service want know how post file stream rest service ?

i using following lines of code post data

if (!string.isnullorempty(postdata) && method == httpverb.post) {     var encoding = new utf8encoding();     var bytes = encoding.getencoding("iso-8859-1").getbytes(postdata);     request.contentlength = bytes.length;      using (var writestream = request.getrequeststream())     {         writestream.write(bytes, 0, bytes.length);     } } 

know how make postdata file stream ? instead of string.

in past have use technique:

private static streamcontent createfilecontent(stream filestream, string filename, string contenttype) {     var filecontent = new streamcontent(filestream);      filecontent.headers.contentdisposition = new contentdispositionheadervalue("form-data")     {         name = "\"files\"",         filename = "\"" + filename + "\""     }; // quotes key here      filecontent.headers.contenttype = new mediatypeheadervalue(contenttype);      return filecontent; } 

and upload via httpclient this:

private async task uploadfile(httpclient client, stream filestream, string filename) {     //httpclient initialized caller     using (var content = new multipartformdatacontent())     {         //file contains xml         content.add(createfilecontent(filestream, filename, "text/xml"));         var resp = await client.postasync("the/rest/endpoint", content);         resp.ensuresuccessstatuscode();     }      return;     // error handling left exercise reader. } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -