Meteor-files: Downloaded file filename

Created on 10 Oct 2016  路  3Comments  路  Source: veliovgroup/Meteor-Files

Question.

I have a file in fs named test.txt

Is it possible that i download that as test01.txt ?

I've read a little about renaming a file when you download it, some said that you can use the download property of an <a>. i tried it but it does not work. i found out that browsers does not support that anymore. i also found something about modifying the header of the file to be downloaded. i remembered that there is something in MF that is related to headers. i checked customer header response but i dont understand fully in the sample if it can be used to modify the header so i can rename the downloaded file. so i made this question.

thank you

question

All 3 comments

Hi @ck23onGithub

You're looking for Content-Disposition header

The default value looks like:

    dispositionName     = "filename=\"#{encodeURIComponent(fileRef.name)}\"; filename=*UTF-8\"#{encodeURIComponent(fileRef.name)}\"; "
    dispositionEncoding = 'charset=utf-8'

    http.response.setHeader 'Content-Disposition', dispositionType + dispositionName + dispositionEncoding

So, you can do something like (_untested_):

var Uploads = new FilesCollection({
  responseHeaders: function(responseCode, fileRef, versionRef, version) {
    var headers = {};
    switch (responseCode) {
      case '206':
        headers['Pragma'] = 'private';
        headers['Trailer'] = 'expires';
        headers['Transfer-Encoding'] = 'chunked';
        break;
      case '400':
        headers['Cache-Control'] = 'no-cache';
        break;
      case '416':
        headers['Content-Range'] = "bytes */" + versionRef.size;
    }
    headers['Connection'] = 'keep-alive';
    headers['Content-Type'] = versionRef.type || 'application/octet-stream';
    headers['Accept-Ranges'] = 'bytes';
    var filename = fileRef.name;
    filename = encodeURIComponent(filename.split('.')[0] + '003.' + filename.split('.')[1]);
    headers['Content-Disposition'] = "filename=\"" + filename + "\"; filename=*UTF-8\"" + filename+ "\"; ";// <-- Your Custom header
    return headers;
  }
});

Thanks for the suggestion. I'll give this a try.

It worked.

thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stefanve picture stefanve  路  4Comments

sidkdbl07 picture sidkdbl07  路  4Comments

nayayan picture nayayan  路  3Comments

dr-dimitru picture dr-dimitru  路  3Comments

RE-N-Y picture RE-N-Y  路  3Comments