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



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" <alanalan@xxxxxxxxxxxxxxxx>
| References: <#Nwo3Un3FHA.3600@xxxxxxxxxxxxxxxxxxxx>
<CXg1MTu3FHA.3220@xxxxxxxxxxxxxxxxxxxxx>
| 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: <#Hcn3Yu3FHA.2816@xxxxxxxxxxxxxxxxxxxx>
| 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!tk2msftngp13.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]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
| news:CXg1MTu3FHA.3220@xxxxxxxxxxxxxxxxxxxxxxxx
| > 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" <alanalan@xxxxxxxxxxxxxxxx>
| > | 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: <#Nwo3Un3FHA.3600@xxxxxxxxxxxxxxxxxxxx>
| > | 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!TK2MSFTNGP12.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
| > |
| > |
| > |
| >
|
|
|

.



Relevant Pages

  • Re: System.Net.WebClient.DownloadFile doesnt work with http attachments
    ... Steven Cheng ... Microsoft Online Support ... Here is the client code I've used to test, ... "How to receive that pdf file at the client side?" ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sending an alert message to logon user in Web Application
    ... As for your question on let the online user in the web application notify ... mode in which the client has no persistent connection with the serverside. ... So if we need to store the message info at the serverside. ... Here is the related reference on DHTML webservice in MSDN: ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Async Programming in asp .Net page as Windows Forms
    ... As for the async processing in ASP.NET you mentioned,here are my ... applicationswhich is request/response based, ... response content back to client. ... there is not relation with the serverside. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Debian replicator boot disk problem
    ... I can connect from an other client in the LAN 192.16.0.12 pretty good. ... say: yes 100% serverside. ... The Clienet is about a 1,44MB Floppy with a realy small kernel on it ...
    (comp.os.linux.misc)
  • RE: Strange exeption when connecting to WebService
    ... when calling webservice. ... Permission issue, the process acount haven't read/write permission to ... serverside issue.(If both the client side and serverside are based on ... Also, as for clientside, the identity of the client app will also need to ...
    (microsoft.public.dotnet.framework.aspnet)