0

I currently have a form that takes in a couple inputs. One of them is an image that I also need to save to a database.

I dont know if this works but my current idea to do this is to use Imgurs API to upload and retrieve the url for said image. Then store that url into my database.

I never had to work with image uploads so this all new to me. In my <input type=file>, I logged the value and I see a path to the image that looks something like this: C:\fakepath.

I am using React and was storing the input values in the state so I could validate the values. I would then call another function that would make an API request to my backend but obviously the file path for the image isnt correct.

How do I pass the actual path to my backend?

Syn
  • 938
  • 1
  • 10
  • 21
  • 1
    Possible duplicate of [how to upload image file and display using express nodejs](https://stackoverflow.com/questions/36477145/how-to-upload-image-file-and-display-using-express-nodejs) – imjared Apr 25 '19 at 22:22
  • Hmm... I don't think theyre the same issue as that one is mainly about Multer.js – Syn Apr 25 '19 at 22:29
  • You'll probably need multer if you want to upload images (assuming you're in a common node.js environment using express) – imjared Apr 25 '19 at 22:37
  • If you're uploading the actual file data from a client, then browsers will hide the true path of the image file for security reasons (the reason for the C:\fakepath). – Informagician Apr 25 '19 at 22:38
  • Is there a better way to go about this then? Or would you say this is how it is normally done. – Syn Apr 25 '19 at 22:41
  • @imjared I am a bit confused on how I would go about that. I am not trying to save the image into my database, I need the path of the file only, so I could pass that path to Imgurs API. – Syn Apr 25 '19 at 22:53
  • Why would you pass any path to Imgur? Imgur will expect an actual file. You then get back a URL pointing to where they saved that file on their servers. And that URL is what you would need to store in your database. But is there any reason you don't just store the uploaded file on your own server, rather than a 3rd party site? – ADyson Apr 25 '19 at 23:00
  • @ADyson Whoops. Read the API incorrectly... I never really touch backend so this stuff is a bit new to me. As of now, I am using my own localhost server but when(if) I ever complete this app, I would need to host this somewhere. There would be a lot of images to store. Is that bad for the server? – Syn Apr 25 '19 at 23:11
  • Yes it could be, it depends on a) the amount of disk space they take up in total, and b) how frequently they will be accessed and how many people might be trying to request them at once. You can buy hosting plans which would be designed for this kind of thing but yes it might be better to offload it to a specialist like Imgur. – ADyson Apr 26 '19 at 06:11

0 Answers0