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

前端 未结 5 961
谎友^
谎友^ 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: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();
                    }
                }
            }
    

提交回复
热议问题