[英]ASP.Net Web Application: How can I save my file to a client machine, (at a location specified by me), on button click?
I've been scouring the web for a while for an answer to this one... would be fantastic if someone could help... 我一直在网上搜寻这个问题的答案...如果有人可以帮忙的话,那太棒了...
I need a button which saves a file contained within my web application to a location on a client machine. 我需要一个按钮,用于将Web应用程序中包含的文件保存到客户端计算机上的某个位置。 I would like this to be done simply on a button-click, ie without the user being prompted for anything such as a saving location.
我希望仅通过单击按钮即可完成此操作,即无需提示用户输入任何内容(例如保存位置)。
I have found a way to do the save part of it, but only if it is the user who uploads the file: 我已经找到一种方法来保存文件的一部分,但前提是要由用户上传文件:
.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);
}
So my question is, how can I change this code from taking the user's uploaded file to taking my file saved in a folder within my application, (eg saved at "_resources/files/temp.txt")? 所以我的问题是,如何将这段代码从获取用户的上传文件更改为保存在应用程序文件夹中的文件(例如,保存在“ _resources / files / temp.txt”中)?
If it isn't obvious, this isn't exactly my forte. 如果不是很明显,那也不是我的强项。 Thanks very much!
非常感谢!
Thanks anyway, but I've just found an answer: 无论如何,谢谢,但是我刚刚找到了答案:
.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.