cancel
Showing results for 
Search instead for 
Did you mean: 

How to download a file using web api.

AZ
Level 3
I want to download a file using Web API. After making request we are getting File data as text in the response content data item.

For your reference I am getting the response as below as text type.

%PDF-1.4 %����
68 0 obj <</Linearized 1/L 1850803/O 70/E 262535/N 13/T 1849323/H [ 916 320]>> endobj
xref
68 31
0000000016 00000 n
0000001236 00000 n
0000001456 00000 n
0000001821 00000 n
0000001957 00000 n
0000002087 00000 n
0000002229 00000 n
0000002972 00000 n
0000003602 00000 n
0000004428 00000 n
0000005000 00000 n
0000005715 00000 n

I am not adding all the content here.

How can we save this content as a original file.

Please help, its an urgent.

Thanks
K

------------------------------
A Z
------------------------------
1 BEST ANSWER

Best Answers

Hi,

hmm, it seems that you will really need to call the WebApi through HTTP Client code (VB or C# code) to get the true binary stream not converted to text. 

a skeleton can look like this (taken from SO)

byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postString);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();

response = (HttpWebResponse)request.GetResponse();
Stream ReceiveStream = response.GetResponseStream();
string filename = "C:\\responseGot.txt";

byte[] buffer = new byte[1024];
FileStream outFile = new FileStream(filename, FileMode.Create);
int bytesRead;
while ((bytesRead = ReceiveStream.Read(buffer, 0, buffer.Length)) != 0)
outFile.Write(buffer, 0, bytesRead);

Regards,

------------------------------
Zdeněk Kabátek
Head of Professional Services
NEOOPS
http://www.neoops.com/
Europe/Prague
------------------------------

View answer in original post

12 REPLIES 12

AmadorYranon
Level 3
Try the Utility - File management - Append to Text File. ...maybe will work..not sure

------------------------------
Amador Yranon
------------------------------

No, it doesn't work.
The actual file is a binary file(pdf, excel,ppt,word), not a text file.

------------------------------
A Z
------------------------------

Sorry, I have not dissected properly your post.

-  VBO Rest API return collection
- VBO HTTP return text

Since end point return binary file e.g. pdf, excel, the only solution that i can think is using Code Stage write code like sample below:

11899.png


------------------------------
Amador Yranon
------------------------------

No, our endpoint taking specific file Id and returning file data as in type text and text mentioned above.
My query is, after receiving file data as text in response data item, how to convert the text into original file.
Can anyone please help.

------------------------------
A Z
------------------------------

Hi,

it is not clear what you are getting then. If you take the content of your Response open data item in Blue Prism containing the text and save it in clipboard and paste in notepad what the result is? The information you have provided is not sufficient to give you more hints.

Regards,

------------------------------
Zdeněk Kabátek
Head of Professional Services
NEOOPS
http://www.neoops.com/
Europe/Prague
------------------------------

Hello,

Please find the below image for your reference.

11903.pngIn the above image, left side data is original Content of Excel File Opened in Notepad, Right side data is from the Web api response content(text) output.

How can I convert the right side data (Response Content) as original Excel File.

Please let me know if any info required.

Thanks,
K


------------------------------
A Z
------------------------------

Hi,

hmm, it seems that you will really need to call the WebApi through HTTP Client code (VB or C# code) to get the true binary stream not converted to text. 

a skeleton can look like this (taken from SO)

byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postString);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();

response = (HttpWebResponse)request.GetResponse();
Stream ReceiveStream = response.GetResponseStream();
string filename = "C:\\responseGot.txt";

byte[] buffer = new byte[1024];
FileStream outFile = new FileStream(filename, FileMode.Create);
int bytesRead;
while ((bytesRead = ReceiveStream.Read(buffer, 0, buffer.Length)) != 0)
outFile.Write(buffer, 0, bytesRead);

Regards,

------------------------------
Zdeněk Kabátek
Head of Professional Services
NEOOPS
http://www.neoops.com/
Europe/Prague
------------------------------

Thanks for the quick reply.

Will let you know after check with this code.

Thanks Again.

Thanks,
K

------------------------------
A Z
------------------------------

@A Z

Could you please let me know if you were able to download the file from WebApi.
If so could you please share your logic.​

------------------------------
Sheela Parthasarathy
Assistant Consultant
TCS
Pacific/Apia
------------------------------