views:

108

answers:

4

I want to download a file and save it locally without any pop ups for user to choose location since its predefined. File format will most probably be pdf. Download file path will be an url and I want to save it locally. Do I go to work like this:

string fileName = "http://mail.example.com/download.asp?saveFile=example.pdf";  

BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open));  

string path = "C:\\Domains\\shared.example.com\\pdf\\example.pdf";  

using (BinaryWriter Writer = new BinaryWriter(new FileStream(path,FileMode.OpenOrCreate)))  
{  
    Writer.Write(binReader);  
}
A: 

You should tag which language you're using.

Also I don't believe browsers would let a file be downloaded without asking the user first as this is a security issue. You could easily throw viruses on to people's computers without them knowing.

eli
Actually, he never mentioned using a browser. And you can download _anything_ from the web without the user knowing, as long nothing stands in the way (Firewall or Virus-Scanner).
Bobby
I'll keep that in mind with my next post.*Note to self to avoid confusion be more descriptive*
GrimDev
A: 

No, that's not working that way...you'll have to use the WebRequest-Class for acquiring the file.

Bobby

Bobby
A: 

Using

new WebClient().DownloadFile("http://url/name.pdf", @"C:\locationOnFilesystem.pdf");

You can use DownloadFileAsync to do this without blocking your main thread. Attach event handlers to the WebClient to report progress, like DownloadProgressChanged and DownloadFileCompleted.

Jan Jongboom
+1  A: 

This is easier:

WebClient wc = new WebClient();
wc.DownloadFile(filename, path);
PoweRoy