2009-11-17 76 views
0

我希望用戶在給定的文本框中輸入文本,並在單擊createNewFile按鈕時彈出SaveAs對話框,用戶應瀏覽位置並根據需要保存文件。保存爲對話框以將文本框內容保存到使用asp.net的新文件

我嘗試過的一些事情,但
1.對話框去
2.在運行時,對話框打開3倍的應用程序背後,意味着它執行3次

回覆POST

protected void btnNewFile_Click(object sender, EventArgs e) 
{ 
    StreamWriter sw = null; 
    try 
    { 
     SaveFileDialog sdlg = new SaveFileDialog(); 
     DialogResult result = sdlg.ShowDialog(); 
     sdlg.InitialDirectory = @"C:\"; 
     sdlg.AddExtension = true; 
     sdlg.CheckPathExists = true; 
     sdlg.CreatePrompt = false; 
     sdlg.OverwritePrompt = true; 
     sdlg.ValidateNames = true; 
     sdlg.ShowHelp = true; 
     sdlg.DefaultExt = "txt"; 
     string file = sdlg.FileName.ToString(); 
     string data = txtNewFile.Text; 

     if (sdlg.ShowDialog() == DialogResult.OK) 
     { 
      sw.WriteLine(txtNewFile.Text); 
      sw.Close(); 
     } 

     if (sdlg.ShowDialog() == DialogResult.Cancel) 
     { sw.Dispose(); } 
    } 
    catch 
    { } 
    finally 
    { 
     if (sw != null) 
     { 
      sw.Close(); 
     } 
    } 
} 

private void Save(string file, string data) 
{ 
    StreamWriter writer = new StreamWriter(file); 
    SaveFileDialog sdlg1 = new SaveFileDialog(); 

    try 
    { 
     if (sdlg1.ShowDialog() == DialogResult.OK) 
     { 
      writer.Write(data); 
      writer.Close(); 
     } 
     else 
      writer.Dispose(); 
    } 
    catch (Exception xp) 
    { 
     MessageBox.Show(xp.Message); 
    } 
    finally 
    { 
     if (writer != null) 
     { 
      writer.Close(); 
     } 
    } 
} 

我試過這個。

+0

發佈您嘗試過的內容,否則我們可能會建議您已嘗試過的某些內容 – ChrisF 2009-11-17 09:11:15

+0

您希望在客戶端或服務器上保存文件的位置? – Murph 2009-11-17 09:50:38

+0

我想爲服務器做,但因爲Iam初學者, 我想爲本地。 – user195114 2009-11-17 10:56:19

回答

0

SaveFileDialog是一個Windows窗體控件,它不能在網站上工作。

當服務器發送一個它默認無法處理的流時,瀏覽器會顯示「你想用這個文件做什麼」對話框 - 不幸的是,大多數瀏覽器可以處理文本流,所以只顯示它們給用戶。

但是這樣的事情應該讓你去:

protected void btnNewFile_Click(object sender, EventArgs e) 
{ 
    // Clear the response buffer: 
    Response.Clear(); 

    // Set the output to plain text: 
    Response.ContentType = "text/plain"; 

    // Send the contents of the textbox to the output stream: 
    Response.Write(txtNewFile.Text); 

    // End the response so we don't get anything else sent (page furniture etc): 
    Response.End(); 
} 

但正如我所說,大多數瀏覽器都可以用純文本處理,所以你可能需要騙瀏覽器,並通過在應用程序類型,但那麼這可能會限制下載在某些機器上的實用性。

0

正如其他人所說,你不能使用SaveFileDialog。如果這樣做,它只會在服務器上可見,而用戶永遠不會看到它。你只能看到它,因爲在你的情況下,服務器和客戶端碰巧是一樣的。

您應該設置HTTP標頭

Content-Disposition: attachment; filename=somefilename.txt 
0

我假設你在的WinForms ENV嘗試這個。 這裏的問題是你在你的代碼中發出三次對.ShowDialog的調用,它彈出三次對話框。你只需要一次調用的ShowDialog然後保存並使用下面

 DialogResult result = sdlg.ShowDialog(); 

     if (result == DialogResult.OK)    
     {     
      sw.WriteLine(data);     
      sw.Close();    
     } 
     else if (result == DialogResult.Cancel) 
     { 

     } 
0

結果onlyas沒有另存爲對話框中的ASP.NET。但是您可以強制您的應用程序在服務器上生成一個文件並將其發回給用戶。

string userProvidedText    = uiTextBox.Text; // this is your textbox 
byte[] userProvidedTextAsBytes  = null; 

if (!string.IsNullOrEmpty(userProvidedText)) { 
    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 
    userProvidedTextAsBytes    = encoding.GetBytes(userProvidedText); 
} 

Response.AppendHeader("Content-Disposition", "attachment; filename=YourFileName.html"); 
Response.ContentType = "text/HTML"; 
Response.BinaryWrite(userProvidedTextAsBytes); 
Response.End(); 

運行此代碼時,應用程序生成「對飛」 YourFileName.html並返回給用戶。此時,瀏覽器攔截結果輸出並詢問用戶如何處理該特定文件。

alt text http://www.cyphersec.com/wp-content/uploads/2009/04/output1.png

注意:使用Response.TransmitFile()如果你想爲以前存儲的文件。其原因是TransmitFile非常有效,因爲它基本上將文件流式傳輸到IIS,包括可能導致文件緩存在內核中(基於IIS的緩存規則)。

+0

非常感謝,這真的很有幫助,比解釋真的非常有價值。 Thanx再次 – user195114 2009-11-18 12:21:37

+0

沒問題,我的榮幸:) – ntze 2009-11-18 14:16:43