简体   繁体   English

在ASP.NET中生成文件托管

[英]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? 有谁知道c夏普或html代码来实现这样的目标?

Thanks. 谢谢。

You can use a file handler ashx for that eg, download.ashx and here is a fast example what you can have inside... 你可以使用文件处理程序ashx ,例如download.ashx ,这里有一个快速的例子,你可以在里面...

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) 生成CSV文件并将其保存到临时文件夹(使用File类实用程序方法获取临时路径)

  2. Use 采用

    Response.ContentType = "text/csv"; Response.ContentType =“ text / csv”; Response.AppendHeader("Content-disposition", "attachment; filename=file.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 调用Response.WriteFile传递文件路径作为参数,如之前生成的,然后是Response.Flush()以确保所有文件都已发送

  4. finally , delete the file finally ,删除该文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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