简体   繁体   中英

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. 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")?

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM