0

I have link of a PDF file located on a website e.g. (https://www.mysite.com/file.pdf). What I need is prompt the user (on Client side) the SaveAs box to choose the file location to save the file.

I tried the SaveFileDialog , but got know that it is only for Windows Forms. and my application is web based.

C# Code

var fileNumber = lblFileNumber.Text;
string fileDownloadLink = "http://www.mysite.com/" + fileNumber + ".pdf";
string fileName = fileNumber + ".pdf";
bool exist = false;
try
   {
      var request = (HttpWebRequest)System.Net.WebRequest.Create(fileDownloadLink);
      using (var response = (HttpWebResponse)request.GetResponse())
       {
          exist = response.StatusCode == HttpStatusCode.OK;
       }
   }
catch
   {
   }

if (exist)
   {
    var wClient = new WebClient();
    wClient.DownloadFile(fileDownloadLink, fileName);
   }

I wrote the above code, it does check if the file is exist on the file location, but

how to prompt user to choose the location to where he want to save file and save on his local hard drive?

AbdulAziz
  • 5,868
  • 14
  • 56
  • 77

1 Answers1

1

It is not possible to do that. When the user wants to download a file, the browser will decide how to show it to the user.

Write a file from disk directly:

HttpContext.Current.Response.TransmitFile(@"C:\yourfile.pdf");

Or from another source, loaded as a byte array:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(bytesOfYourFile);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325