HTTP Request Node: Read filename from content-disposition header when downloading files

HTTP Request Get file link gives me an incomplete binary file. Only Mime Type: image/jpeg is visible. At the same time, the link in the browser downloads the file.
Please help me, how in this situation can I can get a complete binary file - with extension and name?
Thank you.
Regards, Aleksandr.
binary_mime

Information on your n8n setup

  • **n8n version:**0.174.0
  • **Running n8n via [Docker, npm, n8n.cloud, desktop app]:**Docker

I checked with the service where the file is stored - the link is redirected. The link is https://artwork.planfix.ru/?action=getfile&uniqueid=5114808&planfixauth=774de8f8ecc5ae38d70ec563a3302e01
In the node put all the options I could, but the binary file still does not contain the name and extension of the file.
Please help me how to get binary file with name and extension.


How can I get an output like the one on the test file?:

Hey @asuhan, I am not aware of a way to set the filename from the content-disposition header in your screenshot on the fly unfortunately, so I have converted this into a feature request for the time being.

As a workaround, you could manually rename the file like so if needed:

This example code is built upon this snippet provided by a helpful Stack Overflow user and further uses getBinaryData() and setBinaryData(binaryData) to manipulate the binary data received by the HTTP Request node.

Hope this helps!

3 Likes

MutedJam, I have no words to express my admiration and gratitude. You are simply magical!
Thank you so much for all your help!

1 Like

You are most welcome, glad to hear this helps!

can you please send a copy of this code you are using ?

Hi @MotazHakim, it should be visible in the workflow above when you open the Function Item node named Rename.

However, seeing n8n has changed a bit since this was originally posted, I’ve also built a new workflow using the latest node versions, including the current Code node (and a new image URL):

This is the code from the Code node on its own (make sure to set Mode to “Run Once for Each Item” for this to work):

// From https://stackoverflow.com/a/67994693
function getFileName(disposition) {
    const utf8FilenameRegex = /filename\*=UTF-8''([\w%\-\.]+)(?:; ?|$)/i;
    const asciiFilenameRegex = /^filename=(["']?)(.*?[^\\])\1(?:; ?|$)/i;

    let fileName = null;
    if (utf8FilenameRegex.test(disposition)) {
      fileName = decodeURIComponent(utf8FilenameRegex.exec(disposition)[1]);
    } else {
      // prevent ReDos attacks by anchoring the ascii regex to string start and
      //  slicing off everything before 'filename='
      const filenameStart = disposition.toLowerCase().indexOf('filename=');
      if (filenameStart >= 0) {
        const partialDisposition = disposition.slice(filenameStart);
        const matches = asciiFilenameRegex.exec(partialDisposition );
        if (matches != null && matches[2]) {
          fileName = matches[2];
        }
      }
    }
    return fileName;
}


$input.item.binary.data.fileName = getFileName($json.headers['content-disposition']);

return $input.item;

Hope this helps!

1 Like

Native support for content-disposition added in this PR.

3 Likes

New version [email protected] got released which includes the GitHub PR 7032.

1 Like