6

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 ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1734609
  • 111
  • 1
  • 1
  • 5

5 Answers5

5

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();
user1734609
  • 111
  • 1
  • 1
  • 5
2

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


        }
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Mehdi Benkirane
  • 437
  • 4
  • 7
0

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

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • @user1734609 is this what you look for ? :) – Aristos Oct 11 '12 at 06:35
  • I've finished reading through the article, but not exactly :) I have PDF file names in a ListBox. The files from W:/ directory are on a different server, but inside the domain. The listbox is listing all the file names of that directory. The client inside the company when opens the web site can gets the list of PDF files from directory. Then selects one or more and click on save as. And should be able to save on his own PC :) Do you understand the point ? :) – user1734609 Oct 11 '12 at 06:45
  • @user1734609 Yes I understand. You start with that code, and on the point that select the files to show it to the browser, you make the selection from a table with all your domains. I do not know how you can setup that, maybe with share directory, maybe with ftp read, maybe with clone... – Aristos Oct 11 '12 at 06:57
0

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

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();
                }
            }
        }
Jithesh Chandra
  • 860
  • 7
  • 12