471,354 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

System.Net.WebClient.DownloadFile doesn't work with http attachments

Hi,

I have an aspx page at the web server that provides PDF documents for smart
client applications.

Here is the code in aspx page that defines content type:

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition",
"attachment;filename=test.pdf");

I tested the aspx page by using browser and it works just fine.

Now I need to receive that file by using the
System.Net.WebClient.DownloadFile. The problem is that DownloadFile method
returns the following exception:

"Unable to read data from the transport connection: The connection was
closed."

I tested the DownloadFile method to open a direct pdf file like the
following statement:

(new
System.Net.WebClient()).DownloadFile(http://localhost/test.pdf,"c:\\test.pdf");

It works just fine.

How can I use System.Net.WebClient. DownloadFile to download http
attachments?

Any help would be apprecited,
Alan
Nov 19 '05 #1
1 4856
The answer is in your question, but you may not recognize it.

An HTTP Request is for a single resource, identified by the URL of the
resource, or in the case of the WebClient, a URI plus a file descriptor. You
have an ASPX page that returns a file with a couple of special Response
Headers in it. Those Response Headers read:

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=test.pdf");

This header indicates that the client should prompt the client to make a
second request for the file descriptor specified (text.pdf), and that the
Content-Type of that file is binary.

So, what you have to do is read the Response Headers, and make a second
request for that file specified.

IETF RFC 1806 has a detailed explanation of this Response Header:

http://www.ietf.org/rfc/rfc1806.txt

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"A.M-SG" <al******@newsgroup.nospam> wrote in message
news:eY**************@tk2msftngp13.phx.gbl... Hi,

I have an aspx page at the web server that provides PDF documents for
smart
client applications.

Here is the code in aspx page that defines content type:

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition",
"attachment;filename=test.pdf");

I tested the aspx page by using browser and it works just fine.

Now I need to receive that file by using the
System.Net.WebClient.DownloadFile. The problem is that DownloadFile method
returns the following exception:

"Unable to read data from the transport connection: The connection was
closed."

I tested the DownloadFile method to open a direct pdf file like the
following statement:

(new
System.Net.WebClient()).DownloadFile(http://localhost/test.pdf,"c:\\test.pdf");

It works just fine.

How can I use System.Net.WebClient. DownloadFile to download http
attachments?

Any help would be apprecited,
Alan

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by DEWright_CA | last post: by
1 post views Thread by peter.rietmann | last post: by
3 posts views Thread by Chip | last post: by
reply views Thread by Juliano.net | last post: by
8 posts views Thread by =?Utf-8?B?UnVpIE9saXZlaXJh?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.