2

I'm using streaming response on server side - running sinatra.

How can I handle the client side file download (javascript + angular) in a way, that my streaming response would not get fully loaded first and offered only afterwards to be saved to file?

My current impl. looks similar to this one (using FileSaver):

$http.post('/foo', bar)
        .success(function (data) {
            var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
            saveAs(blob, 'A.csv');
        }).

Still I feel like response gets downloaded completely first and only afterwards offered for save.

The thing is, that file downloaded can be huge, so just wanted to provide fast response to end user.

Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81

1 Answers1

1

Well, following answer seems to say all I missed here.

On the client side I ended up with solution, that submits in plain old html way

<form method='POST' action="/foo">
  ...
  <button type="submit" ...>Submit</button>
</form>

where on the server side, I use correct content type:

post '/foo' do
  ...
  content_type 'application/download'

things seem to work much simpler, than I originally expected.

UPDATE:

even more propper way found (as I can specify filename as well as use the correct content type):

post '/foo' do
  ...
  content_type 'text/plain'
  headers["Content-Disposition"] = "attachment; filename=foo.txt"
Community
  • 1
  • 1
Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81