Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#asp.net web api将字节保存为pdf_C#_Asp.net_Api_Pdf_Web - Fatal编程技术网

C#asp.net web api将字节保存为pdf

C#asp.net web api将字节保存为pdf,c#,asp.net,api,pdf,web,C#,Asp.net,Api,Pdf,Web,我目前正在尝试将我的字节保存到pdf。当我从控制器中保存时,一切正常,如下所示 public static async Task<Tuple<byte[], HttpStatusCode>> Pdf(string draftNumber) { byte[] response = null; await Task.Run(() => {

我目前正在尝试将我的字节保存到pdf。当我从控制器中保存时,一切正常,如下所示

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);
            }
问题是,当我在客户端保存文件时,数据似乎没有正确写入pdf。但它与控制器上的字节相同,我不确定这里有什么问题。希望有人能帮我。提前谢谢

“数据似乎没有正确写入pdf。”-你这是什么意思?您是否得到一个空(0字节)文件?您是否得到pdf查看器无法打开的内容?pdf在查看器中的外观是否与预期不同?您的pdf查看器是否收到警告?这只是一种预感吗?
 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);
        }
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);
          
        }