0

After receiving a http request containing as parameter a base64 string of a photo, I want to save the file of this photo in my workspace. I runned the code below, it works without any error, but there is no photo stored in the path mentioned.

const name = req.body.name;

var base64Str = mainImage.replace(/^data:image\/jpeg;base64,/, "");

var optionalObj = {'fileName': name, 'type':'jpeg'};

base64toimage(mainImage,path.join(__dirname,'../../static/images/${name}.jpeg'),optionalObj);

What can be the problem ?

jps
  • 15,760
  • 14
  • 59
  • 71
  • put some debug output `console.log(...)` into your code and see the actual values of `name` and `base64Str`. Are you sure you can access `../../static/images/`? Just try with the current path instead. You also don't use `base64Str` after assigning a value but `mainImage` – jps Sep 09 '20 at 15:16
  • The variables are filled and it's okay with them. How can I know if I have access or not to `../../static/images/` ? – Zohra Belkhiria Sep 10 '20 at 08:14
  • Even when I deleted the name part in the url, the problem persist. Does the fact that the code is deployed to heroku can represent a problem? – Zohra Belkhiria Sep 10 '20 at 09:04
  • Does this answer your question? [NodeJS: Saving a base64-encoded image to disk](https://stackoverflow.com/questions/6926016/nodejs-saving-a-base64-encoded-image-to-disk) – jps Sep 10 '20 at 09:09
  • I suggest to use the code in the linked q/a. The base64toimage lib doesn't make your life easier when you don't even get a proper error message. It's 4 years old and obviously not really active. And the normal js code in the linked answer seems to be not so complicated that you would need a lib to wrap it. – jps Sep 10 '20 at 09:13
  • I tried that and there was the same problem. – Zohra Belkhiria Sep 10 '20 at 09:19
  • didn't you get an error message? I tried with a wrong path and got : `[Error: ENOENT: no such file or directory, open 'D:\xxstatic\images\out.png'] ` and for a forbidden path: `[Error: EPERM: operation not permitted, open 'C:\out.png']` – jps Sep 10 '20 at 09:22
  • `[Error: ENOENT: no such file or directory, open 'D:\xxstatic\images\out.png']` I got an error like that. – Zohra Belkhiria Sep 10 '20 at 10:46
  • at least you get an error. Then the path is wrong. Make sure the directory in which you want to store the file exists. – jps Sep 10 '20 at 10:49
  • I am sure, it exists – Zohra Belkhiria Sep 10 '20 at 13:49
  • Sorry, I have no more ideas.But at least you can hopefully see now that it has nothing to do with base64 and images conversion but simply with reaching a certain path (on heroku) – jps Sep 10 '20 at 14:31
  • Yes, It's. Thanks for your help. – Zohra Belkhiria Sep 10 '20 at 14:33

0 Answers0