簡體   English   中英

將文件寫入Web服務器 - ASP.NET

[英]Writing file to web server - ASP.NET

我只想將TextBox控件的內容寫入Web服務器目錄根目錄中的文件...如何指定它?

請記住,我正在本地測試...它一直在將文件寫入我的程序文件\\ visual studio \\ Common \\ IDE目錄而不是我的項目目錄(我假設root是Web服務器觸發的時候) 。

我的問題與在web.config中指定正確的位置有關嗎? 我試過了,但仍然沒有...

非常感謝...

protected void TestSubmit_ServerClick(object sender, EventArgs e)
    {
        StreamWriter _testData = new StreamWriter("data.txt", true);
        _testData.WriteLine(TextBox1.Text); // Write the file.
        _testData.Close(); // Close the instance of StreamWriter.
        _testData.Dispose(); // Dispose from memory.       
    }
protected void TestSubmit_ServerClick(object sender, EventArgs e)
{
  using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true))
 {
  _testData.WriteLine(TextBox1.Text); // Write the file.
 }         
}

Server.MapPath采用虛擬路徑並返回絕對路徑。 “〜”用於解析應用程序根目錄。

File類中有類似WriteAllText方法,用於File的常見操作。

使用MapPath方法獲取Web應用程序中文件的物理路徑。

File.WriteAllText(Server.MapPath("~/data.txt"), TextBox1.Text);
protected void TestSubmit_ServerClick(object sender, EventArgs e)
{
    using (StreamWriter w = new StreamWriter(Server.MapPath("~/data.txt"), true))
    {
        w.WriteLine(TextBox1.Text); // Write the text
    }
}

請記住,上傳到Web服務器后,您還必須為IUSR帳戶授予對該文件夾的寫入權限。

我個人建議不要允許對根文件夾進行寫訪問,除非你有充分的理由這樣做。 然后你需要小心你允許保存哪種文件,這樣你就不會無意中允許某人編寫自己的ASPX頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM