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.
Information on your n8n setup
**n8n version:**0.174.0
**Running n8n via [Docker, npm, n8n.cloud, desktop app]:**Docker
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:
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;