2009-06-03 2 views
1

사용자가 GridView 내용의 CSV 파일 표현을 "다운로드"할 수있는 옵션을 요청했습니다. 누구든지 서버에 파일을 저장하지 않고이 작업을 수행하는 방법을 알고 있지만 메모리에서 사용자에게 스트리밍하는 방법을 알고 있습니까?ASP.NET 스트림 내용이 파일이 아닌 메모리의

감사

답변

1

다음 코드 ("csv"는 StringBuilder 변수)를 사용하여 StringBuilder를 만들고 Response 객체에 내용을 덤프했습니다.

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

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

context.Response.OutputStream을 사용하십시오.

Here's 예.

2

IHttpHandler를 구현하십시오.

나는, 쉽게 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를하고 출력하기위한 ProcessResponse에 다음과 같은 것을 사용 Response.OutputStream을 전달하여 파일 스트림이 아닌 거기에 쓸 수있게해야합니다.

+0

많은 양의 데이터가있는 경우 response.BufferOutput = false를 추가해야합니다. 이렇게하면 파일 다운로드 상자가 모든 행이 서버에서 버퍼링되기를 기다리는 대신 즉시 팝업됩니다. –

관련 문제