473,509 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4957
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4224
by: James Write | last post by:
Hi, I am using the webclient.downloadfile() method to download a file from a remote web server to the local harddrive. public CopyFile(string sSrcPath, string sDestPath) { WebClient oClient...
6
13251
by: A.M-SG | last post by:
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 =...
8
8994
by: DEWright_CA | last post by:
Why does WebClient.DownloadFile loose my completed path? Ok, I have a function in my app then when my button is clicked it checks to see if the files come from a local drive or a http address....
1
2906
by: peter.rietmann | last post by:
I have been suddenly having a performance problem downloading images from one server (productive) to another (productive) using the following code .. WebClient webClient = new WebClient(); try...
2
3230
by: Lila Godel | last post by:
I am having a problem with the download of web pages via the WebClient.DownloadFile function in the specialized VB.Net 2003 I.E. plug-in I am designing to speed up the work on my latest project. ...
1
4733
by: David Satz | last post by:
Hello--I just upgraded to Visual Studio .NET 2005 and suddenly, all my .NET 1.1 applications that accessed Web sites have broken. For example, this code: WebClient wc = new WebClient();...
3
2272
by: Chip | last post by:
Hi, How do I add a required cookie to my WebClient.DownloadFile() request? Chip
0
1031
by: Juliano.net | last post by:
I have a program that uses WebClient.UploadValues method to send login information to a web site. It works fine on Windows XP, but on Windows Vista the users seems to be sent wrong, because the...
1
1237
by: Curious | last post by:
Hi, In my code, I observe an event. In the event handler, I have: if (mIsMarketOpen == false) { // Execute "CheckHistoricalData" every 15 seconds System.Threading.Timer timer = new...
8
9499
by: =?Utf-8?B?UnVpIE9saXZlaXJh?= | last post by:
WebClient.DownloadFile I am using the WebClient.DownloadFile function to download a file from server to a local client. When I execute the below code, file is created in server and not in...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.