Now in the controller, something like this code:

[System.Web.Http.HttpGet] public HttpResponseMessage exportCSV() { string csvString = model.getAllInCSV(); var response = Request.CreateResponse(); response.StatusCode = HttpStatusCode.OK; response.Content = new StringContent(csvString, Encoding.GetEncoding("WINDOWS-1251")); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "foo.csv" }; return response; } 

csvString is generated in about a couple of minutes, is there a way to immediately upload a file to a user? because Now if the user clicks on the download link, and within the time until the file is formed, it will go to another page of the download, respectively, will not happen.

  • 2
    Can you get CSV gradually as a stream? - VladD
  • Such an approach as here Writing to Output Stream from Action , using FileGeneratingResult may be appropriate. - Primus Singularis
  • @PrimusSingularis is MVC, and here the Web API is Pavel Mayorov

2 answers 2

You can try to do so, but not the fact that it will solve the problem.

 [HttpGet] public HttpResponseMessage exportCSV() { var response = Request.CreateResponse(HttpStatusCode.OK); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "foo.csv" }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content = new PushStreamContent((stream, content, transport) => { try { using (var writer = new StreamWriter(stream, Encoding.GetEncoding("WINDOWS-1251"))) { string csvString = model.getAllInCSV(); writer.Write(csvString); } } finally { stream.Close(); } }); return response; } 

The client can still have time to go to another page until the data is written to stream. I think it makes sense to solve this problem from the client side by sending an ajax request and showing some kind of notification.

    Well, to immediately give it must immediately form. Here, or first receive a command to form, and then give (for example, the first request goes to the formation of the type GenerateCSV, which forms a temporary file, and gives its name, request it with an AYAX, and write to the user what is waiting on the page, and then immediately download) Try to stuff something like a stream into a content and form a file into a stream, not onto a disk.

    • That's about the flow, which way to google, where to see examples? I understand you need to use async, give stream from the controller and write to it from the application side after that? - user213016
    • I don't think this is a good idea. look at this link stackoverflow.com/questions/1072814/… the basic principle is to open the answer and write to the buffer in the same method. If this is done asynchronously, then the result will be - Chad
    • msdn.microsoft.com/en-us/library/... here I see Buffer and BufferOutput as I understand, who are responsible for the method of responding to the client - Chad
    • @Chad why do you give answers about ASP.NET Web Forms when the question is asked about the Web API? - Pavel Mayorov
    • “I don’t think this is a good idea” - this is the best approach you can use - etki