简体   繁体   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?

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.

相关问题 如何从ASP.Net Web应用程序读取/写入客户端计算机的串行端口 - How can read/write serial port of client machine from ASP.Net Web application 如何使用按钮单击在asp.net Web应用程序中显示“另存为”对话框 - How to Make show Save As Dialog box in asp.net web Application using Button click 如何使用Asp.net Web应用程序访问客户端计算机上安装的客户端应用程序(扫描仪或.exe) - how to access client application (Scanner or .exe) which are installed on client machine using Asp.net web application 从Web应用程序操作客户端计算机-ASP.Net - Manipulate Client Machine From Web Application - ASP.Net ASP.NET:创建CSV并将文件保存在客户端计算机上 - ASP.NET: Create CSV and save file on client machine 在另一台机器上运行我的Asp.Net Web应用程序 - run my Asp.Net Web application on another machine 我如何为asp.net Web应用程序构建类体系结构 - How can i build my class architecture for my asp.net web application 如何访问剪贴板图像并将其保存在asp.net Web应用程序的服务器位置? - How to access clipboard image and save it at server location in asp.net web application? 我如何在asp.net Web应用程序中调用嵌入dll中的javascript文件? - How do I call a javascript file that's embedded in a dll in my asp.net web application? 使用ASP.NET和C#在Web应用程序中单击按钮 - Button click in web application with ASP.NET and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM