Mike
Mike

Reputation: 751

How to download a file in c# with a popup asking where to save file

 WebClient webClient = new WebClient();
 webClient.DownloadFile(pdfFilePath, @"D:\DownloadPastPapers.pdf");

I am downloading the pdf file that directly download to specified path but I want to open a popup which asks where to save it (as all normal websites show at the time of downloading) Its a webfroms asp.net application

Upvotes: 1

Views: 7508

Answers (3)

Mike
Mike

Reputation: 751

    pdfFilePath = pdfFilePath + "/DownloadPastPapers.pdf";
    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
    Response.TransmitFile(pdfFilePath);
    Response.End(); 

Upvotes: 0

denys-vega
denys-vega

Reputation: 3697

private string SelectDestinationFile()
{
   var dialog = new SaveFileDialog()
   {
       Title = "Select output file"
       //--Filter can also be defined here
   };

   return dialog.ShowDialog() == true ? dialog.FileName : null;
}

Later

private void DownloadFile(string url)
{
    var filePath = SelectDestinationFile();

    if(string.IsNullOrWhiteSpace(filePath))
       throw new InvalidOperationException("invalid file path");

    using (var client = new WebClient())
       client.DownloadFile(url, filePath);
}

I hope it helps you.

P.S. This is for WPF apps.

Upvotes: 0

MDMalik
MDMalik

Reputation: 4001

You can first popup a SaveFileDialog asking for the save path.

Then use this path in your DownloadFile()

SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
saveFileDialog1.InitialDirectory =     Convert.ToString(Environment.SpecialFolder.MyDocuments); 
saveFileDialog1.Filter = "Your extension here (*.EXT)|*.ext|All Files (*.*)|*.*" ; 
saveFileDialog1.FilterIndex = 1; 

if(saveFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    Console.WriteLine(saveFileDialog1.FileName);//Do what you want here

    WebClient webClient = new WebClient();
   webClient.DownloadFile(pdfFilePath, saveFileDialog1.FileName");
} 

Upvotes: 1

Related Questions