簡體   English   中英

ASP.Net Web應用程序:如何在單擊按鈕時將文件保存到客戶端計算機(在我指定的位置)?

[英]ASP.Net Web Application: How can I save my file to a client machine, (at a location specified by me), on button click?

我一直在網上搜尋這個問題的答案...如果有人可以幫忙的話,那太棒了...

我需要一個按鈕,用於將Web應用程序中包含的文件保存到客戶端計算機上的某個位置。 我希望僅通過單擊按鈕即可完成此操作,即無需提示用戶輸入任何內容(例如保存位置)。

我已經找到一種方法來保存文件的一部分,但前提是要由用戶上傳文件:

.aspx file:
<button onserverclick="SaveButton_Click" runat="server"></button>

// Handles the uploading.
<asp:FileUpload ID="FileUpload1" runat="server"/>



.aspx.cs file:
protected void SaveButton_Click(object sender, EventArgs e)
{   
    // Creates a directory which will be deleted later.
    Directory.CreateDirectory("C:\\temp");

    // Uploads the user's file to the new directory.
    this.FileUpload1.SaveAs("C:\\temp\\" + this.FileUpload1.FileName);
}

所以我的問題是,如何將這段代碼從獲取用戶的上傳文件更改為保存在應用程序文件夾中的文件(例如,保存在“ _resources / files / temp.txt”中)?

如果不是很明顯,那也不是我的強項。 非常感謝!

無論如何,謝謝,但是我剛剛找到了答案:

.aspx file:
<button onserverclick="SaveButton_Click" runat="server"></button>

.aspx.cs file:
protected void SaveButton_Click(object sender, EventArgs e)
{   
    // Creates a directory which will be deleted later.
    Directory.CreateDirectory("C:\\temp");

    // Creates the file I want to save, rather than having it in a location in my web application.
    using (FileStream fs = File.Create("C:\\temp\\temp.txt"))
    {
        Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
        fs.Write(info, 0, info.Length);
    }
}

暫無
暫無

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

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