ASP.NET从内存而不是文件流式传输内容

ASP.NET从内存而不是文件流式传输内容,asp.net,dynamic,download,stream,Asp.net,Dynamic,Download,Stream,用户已请求选择下载GridView内容的csv文件表示。有人知道如何在不将文件保存到服务器而只是将其从内存流式传输给用户的情况下执行此操作吗 感谢我多次使用RKLib导出库,效果非常好,这使用了内存流,可以提供任何数据表,它将导出为csv下载: 我曾多次使用RKLib导出库,效果非常好,它使用内存流,可以提供任何数据表,它将导出为csv下载: 使用context.Response.OutputStream 例如。使用context.Response.OutputStream 一个例子。实现一个I

用户已请求选择下载GridView内容的csv文件表示。有人知道如何在不将文件保存到服务器而只是将其从内存流式传输给用户的情况下执行此操作吗


感谢

我多次使用RKLib导出库,效果非常好,这使用了内存流,可以提供任何数据表,它将导出为csv下载:


我曾多次使用RKLib导出库,效果非常好,它使用内存流,可以提供任何数据表,它将导出为csv下载:


使用context.Response.OutputStream


例如。

使用context.Response.OutputStream


一个例子。

实现一个IHttpHandler

我在ProcessResponse中使用了类似于以下的内容来输出以前在数据库表中构建的CSV

public void ProcessRequest(HttpContext context)
{
    HttpResponse response = context.Response;
    HttpRequest request = context.Request;

    //Get data to output here...

    //Turn off Caching and enforce a content type that will prompt to download/save.
    response.AddHeader("Connection", "close");
    response.AddHeader("Cache-Control", "private");
    response.ContentType = "application/octect-stream";

    //Give the browser a hint at the name of the file.
    response.AddHeader("content-disposition", string.Format("attachment; filename={0}", _filename));

    //Output the CSV here...
    foreach(BatchDTO.BatchRecordsRow row in dtoBatch.BatchRecords)
        response.Output.WriteLine(row.Data);

    response.Flush();
    response.Close();
}

有许多库使生成CSV变得更容易,您应该能够将Response.OutputStream传递给它,让它写入其中,而不是写入文件流。

实现一个IHttpHandler

我在ProcessResponse中使用了类似于以下的内容来输出以前在数据库表中构建的CSV

public void ProcessRequest(HttpContext context)
{
    HttpResponse response = context.Response;
    HttpRequest request = context.Request;

    //Get data to output here...

    //Turn off Caching and enforce a content type that will prompt to download/save.
    response.AddHeader("Connection", "close");
    response.AddHeader("Cache-Control", "private");
    response.ContentType = "application/octect-stream";

    //Give the browser a hint at the name of the file.
    response.AddHeader("content-disposition", string.Format("attachment; filename={0}", _filename));

    //Output the CSV here...
    foreach(BatchDTO.BatchRecordsRow row in dtoBatch.BatchRecords)
        response.Output.WriteLine(row.Data);

    response.Flush();
    response.Close();
}

有许多库使生成CSV更容易,您应该只能够将Response.OutputStream传递给它,让它写入其中而不是文件流。

我创建了一个StringBuilder,并使用以下代码将内容转储到响应对象CSV是StringBuilder变量

    Response.ContentType = @"application/x-msdownload";
    Response.AppendHeader("content-disposition", "attachment; filename=" + FILE_NAME);

    Response.Write(csv.ToString());
    Response.Flush();
    Response.End();

我创建了一个StringBuilder并使用以下代码将内容转储到响应对象csv是StringBuilder变量

    Response.ContentType = @"application/x-msdownload";
    Response.AppendHeader("content-disposition", "attachment; filename=" + FILE_NAME);

    Response.Write(csv.ToString());
    Response.Flush();
    Response.End();

我发现如果你有很多数据,你也应该添加response.BufferOutput=false;因此,文件下载框会立即弹出,而不是等待服务器缓冲所有行;因此,文件下载框会立即弹出,而不是等待服务器缓冲所有行。