0

Lets say you have a web application that uses NodeJS and ReactJS. Both are working on your local machine. NodeJS is accepting some files that are selected from ReactJS GUI with a simple file upload. Like this :

<input type="file" id="fileUploaderInput" ref="fileUploader"/>

You want to pass this file to your NodeJS so that it will perform some big data operations. However, since the Browsers does not have the access to client's File System, you can not take the absolute path of the file selected. Hence you can not inform your NodeJS server about the file. What should be the best approach for this?

I tried reading the file with react and then write it to a path where NodeJS already knows, but it does not seem to me as a best approach.

5
  • Why do you need the absolute file path to upload a file to server? Commented Mar 21, 2019 at 13:12
  • 1
    "Both are working on your local machine" If that’s always the case, why not build a desktop app with e.g. Electron? Commented Mar 21, 2019 at 13:13
  • @jenilchristo Hi, there is no listener in my NodeJS that waits for a file. And I could not decide if it should since my app will always work on local machine with NodeJS and ReactJS. I just need absolue path of the file that lies in somewhere that NodeJS can read with simple var fs = require('fs'); Commented Mar 21, 2019 at 13:20
  • @idmean Hi, thank you, I looked it up and this may be a good solution for my case. But still curious if there exists a simple way to achieve this without losing browser support. Commented Mar 21, 2019 at 13:22
  • if you do persist in web app, say your back-end framework (ExpressJS/AdonisJS/...) Commented Mar 21, 2019 at 14:31

3 Answers 3

1

If your app will always be sitting on your local machine, a desktop app via electron will be a valid approach (as idmean noticed). There you can use the nodejs module fs to work with files even in the frontend, which is enabled via inter-process-communication to the backend. If otherwise the nodejs-server will be sitting in the internet in the future, uploading the file and saving it via the nodejs-server is probably the best approach.

Sign up to request clarification or add additional context in comments.

Comments

0

It is not posible for a browser to get the absolute url of a file uploaded.This would be a security violation .

So you cannot send the absolute file path and read it via fs in node. You can either pass the base64 from client and handle file upload in Node.js or use desktop alternative like Electron.

If your app always runs in local you can also make the download folder predefined say for example (C:/Downloads)in Node.js and always upload files from the same folder, sending the filename from client.So if you select a file with name a.jpg,your Node.js code in local can use the fs to read from path C:/Downloads/a.jpg

Comments

0

It's quite simple. When you upload a file like

<input type="file" />

You get a file blob, you can send this blob to the server using rest API or parse data and then can pass this data to the server.

1 Comment

Can you expand your answer please? What does it mean "upload a file using tag"? An example would be great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.