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

邮差的信 提交于 2019-11-26 14:46:51

问题


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 ?


回答1:


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



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


        }



回答3:


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




回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/12833490/save-dialog-box-to-download-file-saving-file-from-asp-net-server-to-the-client

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