How to modify filename when obtaining file from http?

How to modify filename when obtaining file from http?

1 Like

Hey @CometNet,

You can rename the file name using the Function Node as shown in the example workflow below:

{
  "nodes": [
    {
      "parameters": {
        "url": "https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8dmlld3xlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80",
        "responseFormat": "file",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "let new_file_name = 'New File'\nconst data = Object.assign(items[0].binary.data, {fileName:new_file_name});\nreturn [\n  {\n    json:{},\n    binary: {\n      data:data\n    }\n  }\n]\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Note: I am updating the configuring the file name in the first line of the code in the Function node. Also, this will work only if you’re receiving a single file. For multiple files you’ll have to tweak the function.

1 Like
for (index in items){
  items[index].binary.data = Object.assign(items[index].binary.data, {fileName:"new filename"});
}
return items;

Thank you

2 Likes