简体   繁体   中英

Rest Sharp consuming API that returns raw file

I am making a call to a an API which returns a file. I am told that the API returns...

"The payload is the raw file data with http file type headers."

The API returns the following response in the response.content...

%PDF-1.4\n% ����\n4\n0\nobj\n<<\n/Type\n/Catalog\n/Names\n<<\n/JavaScript\n3\n0\nR\n>>\n/PageLabels\n<<\n/Nums\n[\n0\n<<\n/S\n/D\n/St\n1\n>>\n]\n>>\n/Outlines\n2\n0\nR\n/Pages\n1\n0\nR\n>>\nendobj\n5\n0\nobj\n<<\n/Creator\n(��\0G\0o\0o\0g\0l\0e)\n>>\nendobj\n6\n0\nobj\n<<\n/Type\n/Page\n/Parent\n1\n0\nR\n/MediaBox\n[\n0\n0\n720\n405\n]\n/Contents\n7\n0\nR\n/Resources\n8\n0\nR\n/Annots\n10\n0\nR\n/Group\n<<\n/S\n/Transparency\n/CS\n/DeviceRGB\n>>\n>>\nendobj\n7\n0\nobj\n<<\n/Filter\n/FlateDecode\n/Length\n9\n0\nR\n>>\nstream\nx��SMK\u00031\u0010\rx��!��4�L2�QQ)^��ڃ��

This is not the full response as it is too big to post.

What is the best way to now download that as a file?

So far I have tried the below...

string file = ViewModel.Candidate.Id + "_" + g.ToString() + extension;
byte[] array = Encoding.ASCII.GetBytes(response.Content);
System.IO.File.WriteAllBytes(@"C:\Temp\" + file, array);

but it just creates an empty file

-- UPDATE I can save file but get the below message

在此输入图像描述

If you need to download raw file bytes use DownloadData method

byte[] fileContent = client.DownloadData(request);
//using RestSharp.Extensions;
fileContent.SaveAs(filepath);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM