0

Looking for some guidance here. I am building Nodejs/Express app with MySQL Database. I want to be able to click on the users image on the web page (initial image is generic), select a different image and upload/save new image into MySQL database. I using:

$('#file-input').trigger('click').change (function(){
        alert($('#file-input').val());
    })

I get C:/fakepath... for image location. I would like to how to upload/save selected image to the MySQL database. Connection to database is established, and routes for regular data work just fine.

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
goryef
  • 1,337
  • 3
  • 22
  • 37
  • _"I want to be able to click on the users image on the web page (initial image is generic), select a different image and upload/save new image into MySQL database"_ - You simply can not do it, you can't access to the local file system on behalf of the user, think about the security implications of having this type of feature – Alon Eitan Nov 11 '18 at 17:09
  • I understand all security implications and why path is displayed as C:/fakepath. What I am trying to do is standard feature on every other website i.e. change users profile image. I am trying to implement that. – goryef Nov 11 '18 at 17:15
  • @AlonEitan I think he wants to upload file from browser to server and save it's location in db. – num8er Nov 11 '18 at 17:16
  • @goryef I would recommend You watch videos from here: https://www.youtube.com/results?search_query=nodejs+express+file+upload+example good luck (: – num8er Nov 11 '18 at 17:19
  • Oh, in that case it's fine then. But please google for a tutorial - There are hundreds of those that will help you to achieve this task – Alon Eitan Nov 11 '18 at 17:20
  • @AlonEitan. I started with that. There are so many options. Was hoping for a guidance which one is represents current "best practice" with respect to MySQL. – goryef Nov 11 '18 at 17:21
  • 1
    @goryef I know nothing about node.js (I'm a PHP developer) but isn't [this question](https://stackoverflow.com/questions/15772394/how-to-upload-display-and-save-images-using-node-js-and-express?rq=1) good enough? If you're wondering about saving the image as BLOB in the DB or just the path then [read this](https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay) – Alon Eitan Nov 11 '18 at 17:25
  • @AlonEitan. I can handle BLOB part. I will check the link you mentioned. Thanks – goryef Nov 11 '18 at 17:27

1 Answers1

2

Before answering your question will suggest you to not save image into your MySql or any database, use IPFS, local application directory/folder, or best AWS s3 bucket.

You can use busboy.js NPM module or multer.js NPM module for file upload to server, there's lots of good reason to not save any kind of file in local database.

Now back to how you can save image in database. You can do so by first converting your image to a data format your MySql understand. By default image is binary and depending upon image selected some image binary is so big that even MySql text datatype is small for them. Converting binary to hexadecimal does help but still too big for MySql text datatype. Also you will need multipart/form-data for file upload.

You can easily find "How to upload file in nodeJs?" in a google search. Still if need an example here's one "Upload file using multer.js"

NAVIN
  • 3,193
  • 4
  • 19
  • 32