I am creating a console application that
ContentType is used with the Attachment class to specify the type of content in the attachment. The syntax of the Content-Type header is described in RFC 2045 Section 5.1. RFC 2046 provides detailed information about MIME media types and their parameters. The PDF file will be uploaded using FileUpload control and will be inserted into SQL Server Database Table. A GridView control will display the PDF file present in the SQL Server Database Table along with an option to download the selected PDF file from Database in ASP.Net. TAGs: ASP.Net, SQL Server, GridView. Dim contentType As String.
- Connects to a vendor API to pull voucher numbers for submitted expenses between two dates and
- Downloads a PDF copy of receipts submitted with the expense
The first part, I have working fine. I am able to connect to the Vendor API and parse out the returned XML to create an array of voucher numbers (needed to get the PDF images) using the following code:
Yes, there is likely a better way of doing this, but it works and returns the values I am expecting.
Now, what I am having trouble with, is when I connect back to the API to retrieve the file, I cannot seem to be able to download the file to a specified file path.
I can connect back to the API using
But I cannot seem to write the response to a valid file (PDF)
Here is a screen shot of my Autos window as I step through the code, where I would need to download the file:
My question is, from this point, how do I go about saving the file to my system?
I have tried to take the encoded response I get from doing Console.WriteLine(NewResponseString);
and write it to a file using the System.IO.File.WriteAllLines()
method, using a specified filepath/name, but this results in a blank file. I have also spent some time researching the depths of Google/Stackoverflow, but do not understand how to implement the results I find.
Any and all help would be greatly appreciated.
TimC Content Type Pdf Free
Jeff BeeseJeff Beese4 Answers
So I think you need help with Streams. The returned HttpContent
is actually a System.Net.Http.StreamContent
instance which shows that you are getting content back. Its just a matter of getting the Stream (content) from that instance and saving that to a file.
I respectfully recommend that you do a little reading on how Streams work. This is a common construct in many languages that you will probably have to deal with again in the near future.
IgorIgorFirst of all, are you sure there is a file to begin with? May I suggest using the open source library PdfSharp. I personally use it myself and it works great. As far as downloading the file, maybe this may help you...
Download Synchronously
At first Create StreamReader from NewResponse
Then Define a StremaWriter to write into a file.
Content-type Application/x-pdf
Alternative Approach is
Use this code for download a pdf from the API. It will convert the string data to bytes and provide you the necessary solution.
Draken