Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save dialog box to download file, Saving file from ASP.NET server to the client

I've been searching around the internet, but couldn't find any useful answer.

I have an ASP.NET web site, which is deployed on server. The ASP.NET web site on the server can access a directory called W:/ . The clients in the company can access the web site. The web site lists in a ListBox all the PDF files from the W:/ directory. The client should be able to select PDF files from the listbox and save them to it's local PC by selecting a location for it.

Something like save as file on web pages.

Could you provide me some solution or work around ?

like image 383
user1734609 Avatar asked Oct 11 '12 06:10

user1734609


2 Answers

Finally I've found an article, which Prompts a Save Dialog Box to Download a File from ASP.NET

I post it here, might help somebody else as well and save some time.

 String FileName = "FileName.txt";
 String FilePath = "C:/...."; //Replace this
 System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
 response.ClearContent();
 response.Clear();
 response.ContentType = "text/plain";
 response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
 response.TransmitFile(FilePath);
 response.Flush();
 response.End();
like image 194
user1734609 Avatar answered Sep 20 '22 12:09

user1734609


This is an extension to user1734609's solution that gets a file locally.

To download a file from the server to client:

public void DownloadFile()
        {
            String FileName = "201604112318571964-sample2.txt";
            String FilePath = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/Uploads/" + FileName;
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearContent();
            response.Clear();
            response.ContentType = "text/plain";
            response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
            response.TransmitFile(FilePath);
            response.Flush();
            response.End();


        }
like image 26
Mehdi Benkirane Avatar answered Sep 19 '22 12:09

Mehdi Benkirane