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

47.4k views Asked by At

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 ?

5

There are 5 answers

2
user1734609 On

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();
3
Aristos On

The correct keywords are "File Browser asp.net" to find a lot of examples with source code.

Here is one from codeproject:

http://www.codeproject.com/Articles/301328/ASP-NETUser-Control-File-Browser

1
AudioBubble On

Get file contents in byte[] from W drive and write it to local file.

byte[] data = File.ReadAllBytes(WDriveFilePath)

FileStream file = File.Create(HttpContext.Current.Server.MapPath(MyLocalFile)); 

file.Write(data, 0, data.Length); 
 file.Close(); 
0
Mehdi Benkirane On

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


        }
0
Jithesh Chandra On

I have done something like this to get the file .

protected void btnExportFile_Click(object sender, EventArgs e)
        {
            try
            {
                Thread newThread = new Thread(new ThreadStart(ThreadMethod));
                newThread.SetApartmentState(ApartmentState.STA);
                newThread.Start();     

               // try using threads as you will get a Current thread must be set to single thread apartment (STA) mode before OLE Exception .


            }
            catch (Exception ex)
            {

            }

        }

        static void ThreadMethod()
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    // Code to write the stream goes here.
                    myStream.Close();
                }
            }
        }