Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Download file #67

Closed
ahmadibrahim-c opened this issue Oct 25, 2016 · 13 comments
Closed

Proposal: Download file #67

ahmadibrahim-c opened this issue Oct 25, 2016 · 13 comments
Assignees
Milestone

Comments

@ahmadibrahim-c
Copy link

It will be great if you can provide a way to download file from a route to be something like that

$.download('path/to/file', 'file name', options)
@oscarr-reyes
Copy link

There is an example on how to recieve files from a request in the webpage. Although it would be nice as a separated module feature

@ahmadibrahim-c
Copy link
Author

Hey @Nosthertus , can you give me a link to that example?

@oscarr-reyes
Copy link

Sure. You can see it in http://dietjs.com/tutorials/router#middleware

formidable is used to parse multipart/form-data which is blob type and gather all the files from the submit

@ahmadibrahim-c
Copy link
Author

As far as I understand this shows how to upload file, what I'm looking for is a way for a user to download file when he opens a link, like when he opens a link lilke this https://uploadsite.com/download/12341234 he can then download the file

@oscarr-reyes
Copy link

I was thinking to make that feature for the module i made diet-static-stream but i have gotten pretty busy.. for the moment you would have to read the file and send it in the response.

I have made an example about sending files to the client which could be helpful for you in this comment

@ahmadibrahim-c
Copy link
Author

Thank you so much!!

@adamhalasz adamhalasz self-assigned this Mar 3, 2017
@adamhalasz adamhalasz added this to the Diet v1.0 milestone Mar 3, 2017
@adamhalasz
Copy link
Owner

adamhalasz commented Mar 3, 2017

@ahmadibrahim-c $.download() actually sounds cool 👍 will add it soon

@adamhalasz adamhalasz reopened this Mar 3, 2017
adamhalasz added a commit that referenced this issue Mar 3, 2017
@adamhalasz
Copy link
Owner

As of v0.14.0 you can use $.download(path, filename) as such:

// Register a route that downloads the /image.png as diet_logo.png
app.get('/', function($){
    $.download('./image.png', 'diet_logo.png')
})

For reference check out the simple Download Example:
https://github.com/adamhalasz/diet/tree/master/examples/download

@oscarr-reyes
Copy link

@adamhalasz does this set any special headers to the response?, also.. does this have support for auto mime-type detection?

@adamhalasz
Copy link
Owner

@Nosthertus Yes it sets the content-disposition header. Yes it does detect the mime type since it is using $.sendFile which uses the mime.lookup method from the mime module.

adamhalasz added a commit that referenced this issue Mar 3, 2017
- Bugfix for `$.download` when calling `$.sendFile` send the proper
encoding not the name. #67
@oscarr-reyes
Copy link

@adamhalasz are the headers set after or before calling the download method?.. i ask because there are cases where custom content-type is to be set with a different value which might not exist in mime module

@adamhalasz
Copy link
Owner

@Nosthertus The content-type header is set in $.sendFile which is called within the $.download method. You can take a look:
https://github.com/adamhalasz/diet/blob/master/controllers/protocols/http/signal.js#L65

@ahmadibrahim-c
Copy link
Author

Thanks man! I really needed that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants