Reputation: 71
As per the requirement, I've to call the REST service which will send back an attachment or a file as a binary content of the file in the HTTP body and the appropriate value of the content-type header and i need to open the contents in it's specific editor.
I'm using RestSharp and below is the code for the same: For eg. if the attachment is .doc-
var client = new RestClient(Properties.Settings.Default.RestServicesUrl + Constants.RestTicketServices);
var restRq = new RestRequest(Constants.SearchTicket);
restRq.Method = Method.GET;
restRq.RequestFormat = DataFormat.Json;
restRq.AddParameter("account_id", "10");
client.Authenticator = new HttpBasicAuthenticator(Properties.Settings.Default.RestAuthenticationUserName, Properties.Settings.Default.RestAuthenticationPass);
IRestResponse rest = client.Execute(restRq);
So, after this call:
rest.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8"
rest.Content = has binary data for the word document
and so on....
Basically, I'm looking how i can read the binary contents and open the same in the associated editor.
.xls - Excel .doc - Word .pdf - Adobe and so on.....
Upvotes: 4
Views: 5667
Reputation: 415
It's one year late, but recently i've searched answer for OP question and found it - just replace last line of your code:
byte[] bytes = client.DownloadData(restRq);
If You want to have also an IRestResponse, then You can get RawBytes by:
IRestResponse rest = client.Execute(restRq);
byte[] bytes = rest.RawBytes;
More interesting methods explained in RestSharp source.
Upvotes: 13
Reputation: 16208
You have to save the file (i.e the binary content) to disk first. Then it's simply a matter of opening it:
File.WriteAllBytes(filePath, response.Content);
System.Diagnostics.Process.Start(filePath);
Upvotes: 0