472,331 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 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=" +
fileID.ToString() + ".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 17 '05 #1
6 12942
Hi Alan,

Welcome to MSDN newsgroup.
Regarding on the file downloading through WebClient class problem you
mentioned, seems a bit strange from a general view. It is possible a
machine specific issue. here is a test page's code which could be correctly
used (let client user webbrowser or webclient class to download the file)
on my side:

==================
private void Page_Load(object sender, System.EventArgs e)
{
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/octet-stream";

Response.AppendHeader("Content-Disposition","attachment;filename=test.pdf");

Response.WriteFile(Server.MapPath("~/files/test.pdf"));

Response.End();
}
==================

You can try the above code in your page to see whether it works. Also, you
can run the webclient from multiple client to see whether it is client
specific.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| From: "A.M-SG" <al******@newsgroup.nospam>
| Subject: System.Net.WebClient.DownloadFile doesn't work with http
attachments
| Date: Mon, 31 Oct 2005 18:45:48 -0500
| Lines: 35
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <#N**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: hse-toronto-ppp3484752.sympatico.ca 65.92.97.129
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:133060
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| 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=" +
| fileID.ToString() + ".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 17 '05 #2
Hi Steven,

Firstly thank you for your reply.

The code that you provided is not my question.

My question is: "How to receive that pdf file at the client side?"

The server side is done and it is similar to what you mentioned in your
reply.

I tried to receive the pdf file (test.pdf in your sample) by using
System.Net.WebClient.DownloadFile, but it throws exception.

Regards,
Alan

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:CX**************@TK2MSFTNGXA01.phx.gbl...
Hi Alan,

Welcome to MSDN newsgroup.
Regarding on the file downloading through WebClient class problem you
mentioned, seems a bit strange from a general view. It is possible a
machine specific issue. here is a test page's code which could be
correctly
used (let client user webbrowser or webclient class to download the file)
on my side:

==================
private void Page_Load(object sender, System.EventArgs e)
{
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/octet-stream";

Response.AppendHeader("Content-Disposition","attachment;filename=test.pdf");

Response.WriteFile(Server.MapPath("~/files/test.pdf"));

Response.End();
}
==================

You can try the above code in your page to see whether it works. Also, you
can run the webclient from multiple client to see whether it is client
specific.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| From: "A.M-SG" <al******@newsgroup.nospam>
| Subject: System.Net.WebClient.DownloadFile doesn't work with http
attachments
| Date: Mon, 31 Oct 2005 18:45:48 -0500
| Lines: 35
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <#N**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: hse-toronto-ppp3484752.sympatico.ca 65.92.97.129
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:133060
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| 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=" +
| fileID.ToString() + ".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 17 '05 #3
Hi Alan,

Yes, I know the serverside code won't have big difference , nor does the
client code. That's why I think this should be a environment specific
issue. Here is the client code I've used to test , (both use WebClient and
HttpWebRequest ....) , the code work correctly on my test environment.

=========================
static void Main(string[] args)
{
DownloadByWebClient();
DownloadByHttpWebRequest();
}

static void DownloadByWebClient()
{
string url =
"http://localhost/StevenRoot/ControlWebApp/filedownload.aspx";
string clientfile = @"d:\temp\test_webclient.pdf";

WebClient wc = new WebClient();

wc.DownloadFile(url,clientfile);

}

static void DownloadByHttpWebRequest()
{
string url =
"http://localhost/StevenRoot/ControlWebApp/filedownload.aspx";
string clientfile = @"d:\temp\test_webrequest.pdf";

HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;

req.Method = "GET";
req.KeepAlive = true;

HttpWebResponse rep = req.GetResponse() as HttpWebResponse;

Stream stream = rep.GetResponseStream();

FileStream fs = new FileStream(clientfile, FileMode.Create,
FileAccess.Write);

byte[] buf = new byte[1024];

int i = 0;

while( (i=stream.Read(buf,0,buf.Length))> 0)
{
fs.Write(buf,0,i);
}

fs.Close();

stream.Close();

rep.Close();

}
============================

In addition, you can also try comment the below line in your serverside
code to see whether it helps:

Response.AppendHeader("Content-Disposition", "attachment;filename=" +
fileID.ToString() + ".pdf");

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "A.M-SG" <al******@newsgroup.nospam>
| References: <#N**************@TK2MSFTNGP12.phx.gbl>
<CX**************@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: System.Net.WebClient.DownloadFile doesn't work with http
attachments
| Date: Tue, 1 Nov 2005 08:15:42 -0500
| Lines: 124
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| Message-ID: <#H**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: toronto-hse-ppp4253404.sympatico.ca 70.52.252.114
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:133123
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi Steven,
|
| Firstly thank you for your reply.
|
| The code that you provided is not my question.
|
| My question is: "How to receive that pdf file at the client side?"
|
| The server side is done and it is similar to what you mentioned in your
| reply.
|
| I tried to receive the pdf file (test.pdf in your sample) by using
| System.Net.WebClient.DownloadFile, but it throws exception.
|
| Regards,
| Alan
|
|
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:CX**************@TK2MSFTNGXA01.phx.gbl...
| > Hi Alan,
| >
| > Welcome to MSDN newsgroup.
| > Regarding on the file downloading through WebClient class problem you
| > mentioned, seems a bit strange from a general view. It is possible a
| > machine specific issue. here is a test page's code which could be
| > correctly
| > used (let client user webbrowser or webclient class to download the
file)
| > on my side:
| >
| > ==================
| > private void Page_Load(object sender, System.EventArgs e)
| > {
| > Response.ClearHeaders();
| > Response.ClearContent();
| > Response.ContentType = "application/octet-stream";
| >
| >
Response.AppendHeader("Content-Disposition","attachment;filename=test.pdf");
| >
| > Response.WriteFile(Server.MapPath("~/files/test.pdf"));
| >
| > Response.End();
| > }
| > ==================
| >
| > You can try the above code in your page to see whether it works. Also,
you
| > can run the webclient from multiple client to see whether it is client
| > specific.
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| > --------------------
| > | From: "A.M-SG" <al******@newsgroup.nospam>
| > | Subject: System.Net.WebClient.DownloadFile doesn't work with http
| > attachments
| > | Date: Mon, 31 Oct 2005 18:45:48 -0500
| > | Lines: 35
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | Message-ID: <#N**************@TK2MSFTNGP12.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: hse-toronto-ppp3484752.sympatico.ca 65.92.97.129
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.languages.csharp:133060
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | 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=" +
| > | fileID.ToString() + ".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 17 '05 #4
Hi Alan,

How are you doing on this issue, have you got any further progress? If
there're anything else we can help, please feel free to post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 66213528
| References: <#N**************@TK2MSFTNGP12.phx.gbl>
<CX**************@TK2MSFTNGXA01.phx.gbl>
<#H**************@tk2msftngp13.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: st*****@online.microsoft.com (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Wed, 02 Nov 2005 03:37:50 GMT
| Subject: Re: System.Net.WebClient.DownloadFile doesn't work with http
attachments
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| Message-ID: <8h**************@TK2MSFTNGXA01.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Lines: 167
| Path: TK2MSFTNGXA01.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:133308
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
| Hi Alan,
|
| Yes, I know the serverside code won't have big difference , nor does the
| client code. That's why I think this should be a environment specific
| issue. Here is the client code I've used to test , (both use WebClient
and
| HttpWebRequest ....) , the code work correctly on my test environment.
|
| =========================
| static void Main(string[] args)
| {
|
|
| DownloadByWebClient();
|
|
| DownloadByHttpWebRequest();
| }
|
| static void DownloadByWebClient()
| {
| string url =
| "http://localhost/StevenRoot/ControlWebApp/filedownload.aspx";
| string clientfile = @"d:\temp\test_webclient.pdf";
|
| WebClient wc = new WebClient();
|
| wc.DownloadFile(url,clientfile);
|
| }
|
| static void DownloadByHttpWebRequest()
| {
| string url =
| "http://localhost/StevenRoot/ControlWebApp/filedownload.aspx";
| string clientfile = @"d:\temp\test_webrequest.pdf";
|
| HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
|
| req.Method = "GET";
| req.KeepAlive = true;
|
| HttpWebResponse rep = req.GetResponse() as HttpWebResponse;
|
| Stream stream = rep.GetResponseStream();
|
| FileStream fs = new FileStream(clientfile, FileMode.Create,
| FileAccess.Write);
|
| byte[] buf = new byte[1024];
|
| int i = 0;
|
| while( (i=stream.Read(buf,0,buf.Length))> 0)
| {
| fs.Write(buf,0,i);
| }
|
| fs.Close();
|
| stream.Close();
|
| rep.Close();
|
| }
| ============================
|
| In addition, you can also try comment the below line in your serverside
| code to see whether it helps:
|
| Response.AppendHeader("Content-Disposition", "attachment;filename=" +
| fileID.ToString() + ".pdf");
|
| Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
| --------------------
| | From: "A.M-SG" <al******@newsgroup.nospam>
| | References: <#N**************@TK2MSFTNGP12.phx.gbl>
| <CX**************@TK2MSFTNGXA01.phx.gbl>
| | Subject: Re: System.Net.WebClient.DownloadFile doesn't work with http
| attachments
| | Date: Tue, 1 Nov 2005 08:15:42 -0500
| | Lines: 124
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| | X-RFC2646: Format=Flowed; Original
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| | Message-ID: <#H**************@tk2msftngp13.phx.gbl>
| | Newsgroups: microsoft.public.dotnet.languages.csharp
| | NNTP-Posting-Host: toronto-hse-ppp4253404.sympatico.ca 70.52.252.114
| | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| | Xref: TK2MSFTNGXA01.phx.gbl
| microsoft.public.dotnet.languages.csharp:133123
| | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| |
| | Hi Steven,
| |
| | Firstly thank you for your reply.
| |
| | The code that you provided is not my question.
| |
| | My question is: "How to receive that pdf file at the client side?"
| |
| | The server side is done and it is similar to what you mentioned in your
| | reply.
| |
| | I tried to receive the pdf file (test.pdf in your sample) by using
| | System.Net.WebClient.DownloadFile, but it throws exception.
| |
| | Regards,
| | Alan
| |
| |
| |
| | "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| | news:CX**************@TK2MSFTNGXA01.phx.gbl...
| | > Hi Alan,
| | >
| | > Welcome to MSDN newsgroup.
| | > Regarding on the file downloading through WebClient class problem you
| | > mentioned, seems a bit strange from a general view. It is possible a
| | > machine specific issue. here is a test page's code which could be
| | > correctly
| | > used (let client user webbrowser or webclient class to download the
| file)
| | > on my side:
| | >
| | > ==================
| | > private void Page_Load(object sender, System.EventArgs e)
| | > {
| | > Response.ClearHeaders();
| | > Response.ClearContent();
| | > Response.ContentType = "application/octet-stream";
| | >
| | >
|
Response.AppendHeader("Content-Disposition","attachment;filename=test.pdf");
| | >
| | > Response.WriteFile(Server.MapPath("~/files/test.pdf"));
| | >
| | > Response.End();
| | > }
| | > ==================
| | >
| | > You can try the above code in your page to see whether it works.
Also,
| you
| | > can run the webclient from multiple client to see whether it is client
| | > specific.
| | >
| | > Thanks,
| | >
| | > Steven Cheng
| | > Microsoft Online Support
| | >
| | > Get Secure! www.microsoft.com/security
| | > (This posting is provided "AS IS", with no warranties, and confers no
| | > rights.)
| | >
| | >
| | >
| | >
| | > --------------------
| | > | From: "A.M-SG" <al******@newsgroup.nospam>
| | > | Subject: System.Net.WebClient.DownloadFile doesn't work with http
| | > attachments
| | > | Date: Mon, 31 Oct 2005 18:45:48 -0500
| | > | Lines: 35
| | > | X-Priority: 3
| | > | X-MSMail-Priority: Normal
| | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| | > | X-RFC2646: Format=Flowed; Original
| | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| | > | Message-ID: <#N**************@TK2MSFTNGP12.phx.gbl>
| | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| | > | NNTP-Posting-Host: hse-toronto-ppp3484752.sympatico.ca 65.92.97.129
| | > | Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| | > | Xref: TK2MSFTNGXA01.phx.gbl
| | > microsoft.public.dotnet.languages.csharp:133060
| | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| | > |
| | > | 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="
+
| | > | fileID.ToString() + ".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 17 '05 #5
Hi Steven.

Yes, I got it working. Thank you for your help.

Ali

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:cZ**************@TK2MSFTNGXA01.phx.gbl...
Hi Alan,

How are you doing on this issue, have you got any further progress? If
there're anything else we can help, please feel free to post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 66213528
| References: <#N**************@TK2MSFTNGP12.phx.gbl>
<CX**************@TK2MSFTNGXA01.phx.gbl>
<#H**************@tk2msftngp13.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: st*****@online.microsoft.com (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Wed, 02 Nov 2005 03:37:50 GMT
| Subject: Re: System.Net.WebClient.DownloadFile doesn't work with http
attachments
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| Message-ID: <8h**************@TK2MSFTNGXA01.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Lines: 167
| Path: TK2MSFTNGXA01.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:133308
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
| Hi Alan,
|
| Yes, I know the serverside code won't have big difference , nor does the
| client code. That's why I think this should be a environment specific
| issue. Here is the client code I've used to test , (both use WebClient
and
| HttpWebRequest ....) , the code work correctly on my test environment.
|
| =========================
| static void Main(string[] args)
| {
|
|
| DownloadByWebClient();
|
|
| DownloadByHttpWebRequest();
| }
|
| static void DownloadByWebClient()
| {
| string url =
| "http://localhost/StevenRoot/ControlWebApp/filedownload.aspx";
| string clientfile = @"d:\temp\test_webclient.pdf";
|
| WebClient wc = new WebClient();
|
| wc.DownloadFile(url,clientfile);
|
| }
|
| static void DownloadByHttpWebRequest()
| {
| string url =
| "http://localhost/StevenRoot/ControlWebApp/filedownload.aspx";
| string clientfile = @"d:\temp\test_webrequest.pdf";
|
| HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
|
| req.Method = "GET";
| req.KeepAlive = true;
|
| HttpWebResponse rep = req.GetResponse() as HttpWebResponse;
|
| Stream stream = rep.GetResponseStream();
|
| FileStream fs = new FileStream(clientfile, FileMode.Create,
| FileAccess.Write);
|
| byte[] buf = new byte[1024];
|
| int i = 0;
|
| while( (i=stream.Read(buf,0,buf.Length))> 0)
| {
| fs.Write(buf,0,i);
| }
|
| fs.Close();
|
| stream.Close();
|
| rep.Close();
|
| }
| ============================
|
| In addition, you can also try comment the below line in your serverside
| code to see whether it helps:
|
| Response.AppendHeader("Content-Disposition", "attachment;filename=" +
| fileID.ToString() + ".pdf");
|
| Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
| --------------------
| | From: "A.M-SG" <al******@newsgroup.nospam>
| | References: <#N**************@TK2MSFTNGP12.phx.gbl>
| <CX**************@TK2MSFTNGXA01.phx.gbl>
| | Subject: Re: System.Net.WebClient.DownloadFile doesn't work with http
| attachments
| | Date: Tue, 1 Nov 2005 08:15:42 -0500
| | Lines: 124
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| | X-RFC2646: Format=Flowed; Original
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| | Message-ID: <#H**************@tk2msftngp13.phx.gbl>
| | Newsgroups: microsoft.public.dotnet.languages.csharp
| | NNTP-Posting-Host: toronto-hse-ppp4253404.sympatico.ca 70.52.252.114
| | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| | Xref: TK2MSFTNGXA01.phx.gbl
| microsoft.public.dotnet.languages.csharp:133123
| | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| |
| | Hi Steven,
| |
| | Firstly thank you for your reply.
| |
| | The code that you provided is not my question.
| |
| | My question is: "How to receive that pdf file at the client side?"
| |
| | The server side is done and it is similar to what you mentioned in
your
| | reply.
| |
| | I tried to receive the pdf file (test.pdf in your sample) by using
| | System.Net.WebClient.DownloadFile, but it throws exception.
| |
| | Regards,
| | Alan
| |
| |
| |
| | "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| | news:CX**************@TK2MSFTNGXA01.phx.gbl...
| | > Hi Alan,
| | >
| | > Welcome to MSDN newsgroup.
| | > Regarding on the file downloading through WebClient class problem
you
| | > mentioned, seems a bit strange from a general view. It is possible a
| | > machine specific issue. here is a test page's code which could be
| | > correctly
| | > used (let client user webbrowser or webclient class to download the
| file)
| | > on my side:
| | >
| | > ==================
| | > private void Page_Load(object sender, System.EventArgs e)
| | > {
| | > Response.ClearHeaders();
| | > Response.ClearContent();
| | > Response.ContentType = "application/octet-stream";
| | >
| | >
|
Response.AppendHeader("Content-Disposition","attachment;filename=test.pdf");
| | >
| | > Response.WriteFile(Server.MapPath("~/files/test.pdf"));
| | >
| | > Response.End();
| | > }
| | > ==================
| | >
| | > You can try the above code in your page to see whether it works.
Also,
| you
| | > can run the webclient from multiple client to see whether it is
client
| | > specific.
| | >
| | > Thanks,
| | >
| | > Steven Cheng
| | > Microsoft Online Support
| | >
| | > Get Secure! www.microsoft.com/security
| | > (This posting is provided "AS IS", with no warranties, and confers
no
| | > rights.)
| | >
| | >
| | >
| | >
| | > --------------------
| | > | From: "A.M-SG" <al******@newsgroup.nospam>
| | > | Subject: System.Net.WebClient.DownloadFile doesn't work with http
| | > attachments
| | > | Date: Mon, 31 Oct 2005 18:45:48 -0500
| | > | Lines: 35
| | > | X-Priority: 3
| | > | X-MSMail-Priority: Normal
| | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| | > | X-RFC2646: Format=Flowed; Original
| | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| | > | Message-ID: <#N**************@TK2MSFTNGP12.phx.gbl>
| | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| | > | NNTP-Posting-Host: hse-toronto-ppp3484752.sympatico.ca
65.92.97.129
| | > | Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| | > | Xref: TK2MSFTNGXA01.phx.gbl
| | > microsoft.public.dotnet.languages.csharp:133060
| | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| | > |
| | > | 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="
+
| | > | fileID.ToString() + ".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 17 '05 #6
Thanks for your followup.

Glad that you've got it working.
Have a good day!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "A.M-SG" <al******@newsgroup.nospam>
| References: <#N**************@TK2MSFTNGP12.phx.gbl>
<CX**************@TK2MSFTNGXA01.phx.gbl>
<#H**************@tk2msftngp13.phx.gbl>
<8h**************@TK2MSFTNGXA01.phx.gbl>
<cZ**************@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: System.Net.WebClient.DownloadFile doesn't work with http
attachments
| Date: Mon, 7 Nov 2005 09:54:24 -0500
| Lines: 295
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <#W**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: Toronto-HSE-ppp3736481.sympatico.ca 67.68.49.178
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.languages.csharp:134483
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi Steven.
|
| Yes, I got it working. Thank you for your help.
|
| Ali
|
|
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:cZ**************@TK2MSFTNGXA01.phx.gbl...
| > Hi Alan,
| >
| > How are you doing on this issue, have you got any further progress? If
| > there're anything else we can help, please feel free to post here.
Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| > --------------------
| > | X-Tomcat-ID: 66213528
| > | References: <#N**************@TK2MSFTNGP12.phx.gbl>
| > <CX**************@TK2MSFTNGXA01.phx.gbl>
| > <#H**************@tk2msftngp13.phx.gbl>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain
| > | Content-Transfer-Encoding: 7bit
| > | From: st*****@online.microsoft.com (Steven Cheng[MSFT])
| > | Organization: Microsoft
| > | Date: Wed, 02 Nov 2005 03:37:50 GMT
| > | Subject: Re: System.Net.WebClient.DownloadFile doesn't work with http
| > attachments
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | Message-ID: <8h**************@TK2MSFTNGXA01.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | Lines: 167
| > | Path: TK2MSFTNGXA01.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.languages.csharp:133308
| > | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| > |
| > | Hi Alan,
| > |
| > | Yes, I know the serverside code won't have big difference , nor does
the
| > | client code. That's why I think this should be a environment specific
| > | issue. Here is the client code I've used to test , (both use WebClient
| > and
| > | HttpWebRequest ....) , the code work correctly on my test environment.
| > |
| > | =========================
| > | static void Main(string[] args)
| > | {
| > |
| > |
| > | DownloadByWebClient();
| > |
| > |
| > | DownloadByHttpWebRequest();
| > | }
| > |
| > | static void DownloadByWebClient()
| > | {
| > | string url =
| > | "http://localhost/StevenRoot/ControlWebApp/filedownload.aspx";
| > | string clientfile = @"d:\temp\test_webclient.pdf";
| > |
| > | WebClient wc = new WebClient();
| > |
| > | wc.DownloadFile(url,clientfile);
| > |
| > | }
| > |
| > | static void DownloadByHttpWebRequest()
| > | {
| > | string url =
| > | "http://localhost/StevenRoot/ControlWebApp/filedownload.aspx";
| > | string clientfile = @"d:\temp\test_webrequest.pdf";
| > |
| > | HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
| > |
| > | req.Method = "GET";
| > | req.KeepAlive = true;
| > |
| > | HttpWebResponse rep = req.GetResponse() as HttpWebResponse;
| > |
| > | Stream stream = rep.GetResponseStream();
| > |
| > | FileStream fs = new FileStream(clientfile, FileMode.Create,
| > | FileAccess.Write);
| > |
| > | byte[] buf = new byte[1024];
| > |
| > | int i = 0;
| > |
| > | while( (i=stream.Read(buf,0,buf.Length))> 0)
| > | {
| > | fs.Write(buf,0,i);
| > | }
| > |
| > | fs.Close();
| > |
| > | stream.Close();
| > |
| > | rep.Close();
| > |
| > | }
| > | ============================
| > |
| > | In addition, you can also try comment the below line in your
serverside
| > | code to see whether it helps:
| > |
| > | Response.AppendHeader("Content-Disposition", "attachment;filename=" +
| > | fileID.ToString() + ".pdf");
| > |
| > | Thanks,
| > |
| > | Steven Cheng
| > | Microsoft Online Support
| > |
| > | Get Secure! www.microsoft.com/security
| > | (This posting is provided "AS IS", with no warranties, and confers no
| > | rights.)
| > |
| > | --------------------
| > | | From: "A.M-SG" <al******@newsgroup.nospam>
| > | | References: <#N**************@TK2MSFTNGP12.phx.gbl>
| > | <CX**************@TK2MSFTNGXA01.phx.gbl>
| > | | Subject: Re: System.Net.WebClient.DownloadFile doesn't work with
http
| > | attachments
| > | | Date: Tue, 1 Nov 2005 08:15:42 -0500
| > | | Lines: 124
| > | | X-Priority: 3
| > | | X-MSMail-Priority: Normal
| > | | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| > | | X-RFC2646: Format=Flowed; Original
| > | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| > | | Message-ID: <#H**************@tk2msftngp13.phx.gbl>
| > | | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | | NNTP-Posting-Host: toronto-hse-ppp4253404.sympatico.ca 70.52.252.114
| > | | Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| > | | Xref: TK2MSFTNGXA01.phx.gbl
| > | microsoft.public.dotnet.languages.csharp:133123
| > | | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | |
| > | | Hi Steven,
| > | |
| > | | Firstly thank you for your reply.
| > | |
| > | | The code that you provided is not my question.
| > | |
| > | | My question is: "How to receive that pdf file at the client side?"
| > | |
| > | | The server side is done and it is similar to what you mentioned in
| > your
| > | | reply.
| > | |
| > | | I tried to receive the pdf file (test.pdf in your sample) by using
| > | | System.Net.WebClient.DownloadFile, but it throws exception.
| > | |
| > | | Regards,
| > | | Alan
| > | |
| > | |
| > | |
| > | | "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| > | | news:CX**************@TK2MSFTNGXA01.phx.gbl...
| > | | > Hi Alan,
| > | | >
| > | | > Welcome to MSDN newsgroup.
| > | | > Regarding on the file downloading through WebClient class problem
| > you
| > | | > mentioned, seems a bit strange from a general view. It is
possible a
| > | | > machine specific issue. here is a test page's code which could be
| > | | > correctly
| > | | > used (let client user webbrowser or webclient class to download
the
| > | file)
| > | | > on my side:
| > | | >
| > | | > ==================
| > | | > private void Page_Load(object sender, System.EventArgs e)
| > | | > {
| > | | > Response.ClearHeaders();
| > | | > Response.ClearContent();
| > | | > Response.ContentType = "application/octet-stream";
| > | | >
| > | | >
| > |
| >
Response.AppendHeader("Content-Disposition","attachment;filename=test.pdf");
| > | | >
| > | | > Response.WriteFile(Server.MapPath("~/files/test.pdf"));
| > | | >
| > | | > Response.End();
| > | | > }
| > | | > ==================
| > | | >
| > | | > You can try the above code in your page to see whether it works.
| > Also,
| > | you
| > | | > can run the webclient from multiple client to see whether it is
| > client
| > | | > specific.
| > | | >
| > | | > Thanks,
| > | | >
| > | | > Steven Cheng
| > | | > Microsoft Online Support
| > | | >
| > | | > Get Secure! www.microsoft.com/security
| > | | > (This posting is provided "AS IS", with no warranties, and
confers
| > no
| > | | > rights.)
| > | | >
| > | | >
| > | | >
| > | | >
| > | | > --------------------
| > | | > | From: "A.M-SG" <al******@newsgroup.nospam>
| > | | > | Subject: System.Net.WebClient.DownloadFile doesn't work with
http
| > | | > attachments
| > | | > | Date: Mon, 31 Oct 2005 18:45:48 -0500
| > | | > | Lines: 35
| > | | > | X-Priority: 3
| > | | > | X-MSMail-Priority: Normal
| > | | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | | > | X-RFC2646: Format=Flowed; Original
| > | | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | | > | Message-ID: <#N**************@TK2MSFTNGP12.phx.gbl>
| > | | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | | > | NNTP-Posting-Host: hse-toronto-ppp3484752.sympatico.ca
| > 65.92.97.129
| > | | > | Path:
| > TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| > | | > | Xref: TK2MSFTNGXA01.phx.gbl
| > | | > microsoft.public.dotnet.languages.csharp:133060
| > | | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | | > |
| > | | > | 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="
| > +
| > | | > | fileID.ToString() + ".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 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Justin Malloy | last post by:
I am using the System.Net.Webclient to try and download an XML file from a website but am receiving a HTTP protocol error when running the...
1
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...
0
by: pascal_dumont | last post by:
Hi all, I have a problem with downloading files by using WebClient wc = new WebClient(); wc.DownloadFile("http://some_url.com/some_file.gif",...
5
by: Vitling | last post by:
For no apparent reason, a NullReference exception is thrown in system.dll (System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback). Since...
8
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...
1
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...
1
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...
1
by: Mike | last post by:
I am using PowerShell to download an XML file into a string using the DownloadString(URI) method of System.Net.WebClient object. Sample commands...
8
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.