ASP.Net Web应用程序:单击按钮,如何将文件保存到客户端计算机(在我指定的位置)
本文关键字:计算机 客户端 位置 保存 文件 应用程序 Web Net 单击 按钮 ASP | 更新日期: 2023-09-27 18:22:46
我已经在网上搜索了一段时间,想找到这个问题的答案。。。如果有人能帮忙那就太好了。。。
我需要一个按钮,它可以将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);
}
}