简体   繁体   中英

generated file hosting in ASP.NET

I'd like to prompt the user to download a file, generate it once they accept, and delete the file once they have made the transfer (if they accept the download.)

Does anyone know the c sharp or html code to achieve something like this?

Thanks.

You can use a file handler ashx for that eg, download.ashx and here is a fast example what you can have inside...

public void ProcessRequest(HttpContext context)
{
    // example for the csv
    context.Response.ContentType = "text/html";
    // what is the file name that the user see to save
    context.Response.AppendHeader("Content-disposition", "attachment; filename=" + cFileNameToShowAndDownload);

    context.Response.Write("here is your text to send");

    context.Response.Flush();
}

When the user accepts the download (ie. click on a button):

  1. Generate CSV file and save it to temporary folder (use File class utility methods to get a temporary path)

  2. Use

    Response.ContentType = "text/csv"; Response.AppendHeader("Content-disposition", "attachment; filename=file.csv");

  3. Call Response.WriteFile passing the file path as argument, as generated before, then Response.Flush() to make sure all the file was sent

  4. finally , delete the file

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