redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

How to respond with a file? #64

Closed austincummings closed 9 years ago

austincummings commented 9 years ago

Hey @luizmineo,

I need to have a route that is capable of returning a file from the file system. I currently have one that returns a Future<File> as new File(filePath). But it seems that everytime I try to request the route it sends a completely empty response.

Any help with this would be greatly appreciated.

Thanks

austincummings commented 9 years ago

Actually it seems it's responding with the file. But not as a download in the browser. Any idea on that?

luizmineo commented 9 years ago

Hi,

To force the browser to handle the response as a downloadable file, you can set the content-disposition: attachment; header. There are some ways to do that. One option is to use an interceptor:

@app.Interceptor("/files/.*")
downloadFileInterceptor() {
  app.chain.next(() => app.response.change(headers: {
    "Content-Disposition": "attachment; filename=\"${app.request.url.pathSegments.last}\""
  }));
}

@app.Route("/files/:filename")
Future<File> downloadFile(String filename) {
  //create and return a File object
}

That way, the user can send a request to /files/report.pdf, to download a report.pdf file, for example.

Another option is to directly build and return a shelf.Response object, instead of returning a file.

Let me know if that helps.

luizmineo commented 9 years ago

Closing this issue, but let me know if you still has problems or doubts.