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

前端 未结 5 943
谎友^
谎友^ 2020-12-01 21:59

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

相关标签:
5条回答
  • 2020-12-01 22:39

    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 讨论(0)
  • 2020-12-01 22:42

    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();
    
    0 讨论(0)
  • 2020-12-01 22:45

    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

    0 讨论(0)
  • 2020-12-01 22:47

    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 讨论(0)
  • 2020-12-01 22:52

    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();
                    }
                }
            }
    
    0 讨论(0)
提交回复
热议问题