I'm currently trying to save my bytes to a pdf. All works fine when I'm saving from the controller like shown below
public static async Task<Tuple<byte[], HttpStatusCode>> Pdf(string draftNumber)
{
byte[] response = null;
await Task.Run(() =>
{
var client = new RestClient(string.Format("https://restapi.e-conomic.com/invoices/drafts/{0}/pdf", draftNumber));
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("X-AgreementGrantToken", Variables.XAgreementGrantToken);
request.AddHeader("X-AppSecretToken", Variables.XAppSecretToken);
request.AddHeader("Content-Type", "application/octet-stream");
request.AddParameter("application/octet-stream", ParameterType.RequestBody);
response = client.DownloadData(request);
// This saves the pdf file without problems
//File.WriteAllBytes(@"C:\Users\Nicklas\Desktop\hello.pdf", response);
});
return new Tuple<byte[], HttpStatusCode>(response, HttpStatusCode.OK);
}
The only issue is that I want to save the pdf file on client side. In order to do so, I'm getting the response bytes from the controller like below.
public static async Task<Tuple<byte[], HttpStatusCode>> GetPdf(int customerNumber, int draftNumber)
{
byte[] response = null;
await Task.Run(() =>
{
var client = new RestClient(string.Format(Variables.host + "/Economic/Customers/{0}/Invoices/{1}/pdf", customerNumber, draftNumber));
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/octet-stream");
request.AddHeader("Authorization", "Bearer " + Variables.token);
request.AddParameter("application/octet-stream", ParameterType.RequestBody);
response = client.DownloadData(request);
Console.WriteLine(response);
});
return new Tuple<byte[], HttpStatusCode>(response, HttpStatusCode.OK);
}
And here I'm try to save the bytes to the pdf:
private async void DownloadPdf()
{
var s = await API.Economic.Invoice.GetPdf(Convert.ToInt32(_clientId), 6);
byte[] bytes = s.Item1;
File.WriteAllBytes(@"C:\Users\Nicklas\Desktop\hello.pdf", bytes);
}
The issue is that when I'm saving the file on client side, it seems that the data is not being written correctly to the pdf. But it is the same bytes as on the controller, I'm not sure what's the issue here. Hope someone can help me out. Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire