[{"data":1,"prerenderedAt":460},["ShallowReactive",2],{"blog-\u002Fblog\u002F2024\u002F01\u002Fsend-a-file":3},{"id":4,"title":5,"body":6,"description":12,"extension":450,"meta":451,"navigation":122,"path":456,"seo":457,"stem":458,"__hash__":459},"blog\u002Fblog\u002F2024\u002F01\u002Fsend-a-file.md","Send a File to Node-RED",{"type":7,"value":8,"toc":441},"minimark",[9,13,18,21,24,28,31,50,54,62,73,76,102,105,222,229,235,240,243,252,255,262,265,270,273,300,304,314,322,326,330,333,348,351,355,358,374,377,386,393,397,405,408,415,422,425,429,437],[10,11,12],"p",{},"Have you ever needed to send a CSV file to your Node-RED instance? This file can go on to populate a shift schedule, product specifications, or some other configuration file that is used. In this guide, we provide a couple of options to upload the data to your Node-RED for further processing and to organize the data to be sent on or used.",[14,15,17],"h2",{"id":16},"why-would-you-need-to-send-a-file-to-node-red","Why would you need to send a file to Node-RED?",[10,19,20],{},"Often times it is necessary to update lookup tables in a SQL database, but you don't necessarily want to give access to everyone to edit the database, nor do you want to have to do it all yourself. This can often be seen when new products are introduced into a manufacturing facility. It may not be often, but enough that it warrants its own application. This process will guide you in a way that will enable your teammates to upload the files to the system themselves.",[10,22,23],{},"Furthermore, on the management layer of most companies, Excel and Google Sheets are the go-to tools to perform data collection tasks. Getting management involved in processes might require you to build an import feature for them. Asking your manager to \"Save as\" CSV is much easier than teaching them SQL!",[14,25,27],{"id":26},"_2-ways-to-send-a-file-to-node-red","2 Ways to send a file to Node-RED",[10,29,30],{},"There are many approaches that can be taken when solving this. We are going to go over 2 here.",[32,33,34,43],"ol",{},[35,36,37,42],"li",{},[38,39,41],"a",{"href":40},"#simple-python-script","Simple Python Script"," - Simple script that will be shared below. It is a simple Python application that allows the user to send a file with a simple command, but this might require a little more technical skills that the end user may not feel comfortable with.",[35,44,45,49],{},[38,46,48],{"href":47},"#stand-alone-web-application","Stand Alone Web Application"," - A web-based application that allows the user to upload files to a browser with a selectable endpoint.",[51,52,41],"h3",{"id":53},"simple-python-script",[10,55,56,57,61],{},"This simple Python script sends a file to a Node-RED flow.  The flow that will work with this script can be seen ",[38,58,60],{"href":59},"#node-red-ingress","here",".",[10,63,64,65,69,70,61],{},"The script requires ",[66,67,68],"strong",{},"requests"," and ",[66,71,72],{},"Python 3.x",[10,74,75],{},"Install requests:",[77,78,83],"pre",{"className":79,"code":80,"language":81,"meta":82,"style":82},"language-bash shiki shiki-themes github-light github-dark","pip install requests\n","bash","",[84,85,86],"code",{"__ignoreMap":82},[87,88,91,95,99],"span",{"class":89,"line":90},"line",1,[87,92,94],{"class":93},"sScJk","pip",[87,96,98],{"class":97},"sZZnC"," install",[87,100,101],{"class":97}," requests\n",[10,103,104],{},"Create a file called run.py and paste the contents into the file.",[77,106,110],{"className":107,"code":108,"language":109,"meta":82,"style":82},"language-python shiki shiki-themes github-light github-dark","import requests\n\ndef send_file(nodered_url, file_path):\n    # Open the file in binary mode\n    with open(file_path, 'rb') as file:\n        files = {'file': (file.name, file, 'multipart\u002Fform-data')}\n        response = requests.post(nodered_url, files=files)\n\n    return response\n\n# Update the ip address and port of your Node-RED instance\nnodered_url = 'http:\u002F\u002Flocalhost:1880\u002Ffileupload'\n\n# Update the location of your file\nfile_path = 'C:\u002FUsers\u002FmyUser\u002FDownloads\u002FshiftSchedule.csv'\n\nresponse = send_file(nodered_url, file_path)\nprint(f\"Response Status Code: {response.status_code}\")\nprint(f\"Response Body: {response.text}\")\n","python",[84,111,112,117,124,130,136,142,148,154,159,165,170,176,182,187,193,199,204,210,216],{"__ignoreMap":82},[87,113,114],{"class":89,"line":90},[87,115,116],{},"import requests\n",[87,118,120],{"class":89,"line":119},2,[87,121,123],{"emptyLinePlaceholder":122},true,"\n",[87,125,127],{"class":89,"line":126},3,[87,128,129],{},"def send_file(nodered_url, file_path):\n",[87,131,133],{"class":89,"line":132},4,[87,134,135],{},"    # Open the file in binary mode\n",[87,137,139],{"class":89,"line":138},5,[87,140,141],{},"    with open(file_path, 'rb') as file:\n",[87,143,145],{"class":89,"line":144},6,[87,146,147],{},"        files = {'file': (file.name, file, 'multipart\u002Fform-data')}\n",[87,149,151],{"class":89,"line":150},7,[87,152,153],{},"        response = requests.post(nodered_url, files=files)\n",[87,155,157],{"class":89,"line":156},8,[87,158,123],{"emptyLinePlaceholder":122},[87,160,162],{"class":89,"line":161},9,[87,163,164],{},"    return response\n",[87,166,168],{"class":89,"line":167},10,[87,169,123],{"emptyLinePlaceholder":122},[87,171,173],{"class":89,"line":172},11,[87,174,175],{},"# Update the ip address and port of your Node-RED instance\n",[87,177,179],{"class":89,"line":178},12,[87,180,181],{},"nodered_url = 'http:\u002F\u002Flocalhost:1880\u002Ffileupload'\n",[87,183,185],{"class":89,"line":184},13,[87,186,123],{"emptyLinePlaceholder":122},[87,188,190],{"class":89,"line":189},14,[87,191,192],{},"# Update the location of your file\n",[87,194,196],{"class":89,"line":195},15,[87,197,198],{},"file_path = 'C:\u002FUsers\u002FmyUser\u002FDownloads\u002FshiftSchedule.csv'\n",[87,200,202],{"class":89,"line":201},16,[87,203,123],{"emptyLinePlaceholder":122},[87,205,207],{"class":89,"line":206},17,[87,208,209],{},"response = send_file(nodered_url, file_path)\n",[87,211,213],{"class":89,"line":212},18,[87,214,215],{},"print(f\"Response Status Code: {response.status_code}\")\n",[87,217,219],{"class":89,"line":218},19,[87,220,221],{},"print(f\"Response Body: {response.text}\")\n",[10,223,224,225,228],{},"Update the ",[66,226,227],{},"nodered_url"," to the location of the Node-RED instance.  Be sure to adjust the port if the default port of 1880 isn't being used.",[10,230,224,231,234],{},[66,232,233],{},"file_path"," with the path to where the file to be uploaded will be located.",[10,236,237],{},[66,238,239],{},"Save",[10,241,242],{},"To run:",[77,244,246],{"className":107,"code":245,"language":109,"meta":82,"style":82},"python run.py\n",[84,247,248],{"__ignoreMap":82},[87,249,250],{"class":89,"line":90},[87,251,245],{},[51,253,48],{"id":254},"stand-alone-web-application",[10,256,257],{},[258,259],"img",{"alt":260,"src":261},"csv upload application","\u002Fblog\u002F2024\u002F01\u002Fimages\u002Fcsv_upload_app.png",[10,263,264],{},"This stand-alone web application can be run on either Windows or Linux, .bat for Windows, and .sh for Linux.",[266,267,269],"h4",{"id":268},"installation","Installation",[10,271,272],{},"Clone the repository and navigate to the directory:",[77,274,276],{"className":79,"code":275,"language":81,"meta":82,"style":82},"git clone https:\u002F\u002Fgithub.com\u002Fgdziuba\u002FFF_Send-File-to-NR.git && cd FF_Send-File-to-NR\n",[84,277,278],{"__ignoreMap":82},[87,279,280,283,286,289,293,297],{"class":89,"line":90},[87,281,282],{"class":93},"git",[87,284,285],{"class":97}," clone",[87,287,288],{"class":97}," https:\u002F\u002Fgithub.com\u002Fgdziuba\u002FFF_Send-File-to-NR.git",[87,290,292],{"class":291},"sVt8B"," && ",[87,294,296],{"class":295},"sj4cs","cd",[87,298,299],{"class":97}," FF_Send-File-to-NR\n",[266,301,303],{"id":302},"configuration","Configuration",[10,305,306,307,313],{},"Edit the lines in the body of ",[38,308,312],{"href":309,"rel":310},"https:\u002F\u002Fgithub.com\u002Fgdziuba\u002FFF_Send-File-to-NR\u002Fblob\u002F21214f88c6c4536f49efb88cf5f84bf52071a88b\u002Ftemplates\u002Findex.html#L69",[311],"nofollow","index.html"," to include the endpoints to which you would like to send the files.",[77,315,320],{"className":316,"code":318,"language":319},[317],"language-text","\u003Coption value=\"http:\u002F\u002Flocalhost:1880\u002Ffileupload\">CSV File Upload\u003C\u002Foption>\n","text",[84,321,318],{"__ignoreMap":82},[51,323,325],{"id":324},"operating-systems","Operating Systems",[266,327,329],{"id":328},"windows","Windows",[10,331,332],{},"Run the script:",[77,334,336],{"className":79,"code":335,"language":81,"meta":82,"style":82},".\\start_app.bat\n",[84,337,338],{"__ignoreMap":82},[87,339,340,342,345],{"class":89,"line":90},[87,341,61],{"class":295},[87,343,344],{"class":291},"\\",[87,346,347],{"class":97},"start_app.bat\n",[10,349,350],{},"This will install if necessary, start the Flask Application, and take you to localhost:5000 on the browser.",[266,352,354],{"id":353},"linux","Linux",[10,356,357],{},"Make the script executable by running running:",[77,359,361],{"className":79,"code":360,"language":81,"meta":82,"style":82},"chmod +x setup_and_run.sh\n",[84,362,363],{"__ignoreMap":82},[87,364,365,368,371],{"class":89,"line":90},[87,366,367],{"class":93},"chmod",[87,369,370],{"class":97}," +x",[87,372,373],{"class":97}," setup_and_run.sh\n",[10,375,376],{},"Then run the application with:",[77,378,380],{"className":79,"code":379,"language":81,"meta":82,"style":82},".\u002Fsetup_and_run.sh\n",[84,381,382],{"__ignoreMap":82},[87,383,384],{"class":89,"line":90},[87,385,379],{"class":93},[10,387,388,389,392],{},"To access the application, open a browser to the ",[66,390,391],{},"\u003Cnode-red-host-ip>:5000"," of the running application.",[266,394,396],{"id":395},"node-red-ingress","Node-RED Ingress",[398,399],"iframe",{"width":400,"height":401,"src":402,"allow":403,"style":404},"100%","225px","https:\u002F\u002Fflows.nodered.org\u002Fflow\u002Feffb53752e5d6f767b3c7e5d41a4a6e8\u002Fshare?height=100","clipboard-read; clipboard-write","border: none;",[10,406,407],{},"Once we have a file ready to be sent, we now need to configure the receiving side in Node-RED. In this example, we are leveraging a CSV formatted file and then converting it to be used at a later time.",[10,409,410,411,61],{},"A link to the flow can be found ",[38,412,60],{"href":413,"rel":414},"https:\u002F\u002Fflows.nodered.org\u002Fflow\u002Feffb53752e5d6f767b3c7e5d41a4a6e8",[311],[10,416,417,418,61],{},"To import the flow, follow these ",[38,419,421],{"href":420},"\u002Fblog\u002F2023\u002F03\u002F3-quick-node-red-tips-5\u002F#1.-copy-and-share-your-flows-using-export-and-import","instructions",[10,423,424],{},"A Simple HTTP In node can be used in the form of a Post, ensuring the configuration allows for a file.",[14,426,428],{"id":427},"wanna-import-it-directly-into-your-node-red-instance-via-a-dashboard","Wanna import it directly into your Node-RED instance via a Dashboard?",[10,430,431,432,436],{},"Check out this ",[38,433,435],{"href":434},"\u002Fblog\u002F2024\u002F01\u002Fimport-a-file","blog"," on how to directly import a file into a Node-RED instance via Dashboard 2.0.",[438,439,440],"style",{},"html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":82,"searchDepth":119,"depth":119,"links":442},[443,444,449],{"id":16,"depth":119,"text":17},{"id":26,"depth":119,"text":27,"children":445},[446,447,448],{"id":53,"depth":126,"text":41},{"id":254,"depth":126,"text":48},{"id":324,"depth":126,"text":325},{"id":427,"depth":119,"text":428},"md",{"navTitle":5,"excerpt":452},{"type":7,"value":453},[454],[10,455,12],{},"\u002Fblog\u002F2024\u002F01\u002Fsend-a-file",{"title":5,"description":12},"blog\u002F2024\u002F01\u002Fsend-a-file","nacx9cj5mGEERmZ4Zdym7Vp7816HKh7BSLjkn4pU610",1780070551372]