Download PDF file from Web location and Prompt user SaveAs box on client in web-Application ASP C#

旧城冷巷雨未停 提交于 2019-12-11 16:22:26

问题


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?


回答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();


来源:https://stackoverflow.com/questions/21757831/download-pdf-file-from-web-location-and-prompt-user-saveas-box-on-client-in-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!