0

I am working on a angular app. I have two images namely X.png and Y.png. I want to copy these images from my assets folder to a folder in C drive c:\users\images. The destination path also I am recieving as variable. How can I do that ?I was trying to write a function something like this in node.

function moveIMage(ImageStyle, destination) { const fs = require('fs'); // destination will be created or overwritten by default. fs.copyFile('src\assests\images\' + ImageStyle, 'destination'); } 

value of Image style can be X or Y and on the basis of this I want to pick image and move it to destination. Above code is not working. how can I do that?

2
  • the javascript code in your angular app runs in the browser, not on the server, so that code cannot copy files or require the 'fs' library. That work needs to be done by code running on the server. that example code is node.js code - so it would have to be part of the node.js application (possibly using express) - your angular application would need to make some type of request to the nodejs application to trigger that code. Commented Apr 17, 2020 at 5:25
  • Angular is a client side framework. Which means it runs on browser and you should not expect an API from angular to access on disc. Do it with Api service with sending parameters Commented Apr 17, 2020 at 10:02

1 Answer 1

0

You are missing a callback. rewrite your function as below

const fs = require('fs');// I prefer require statements outside of function function moveIMage(ImageStyle, destination) { // destination will be created or overwritten by default. fs.copyFile('src\assests\images\' + ImageStyle, destination, (err) => { if(err) throw err; console.log('copied image') }); } 

also this will copy file not deleting original file, if you want to movie file use fs.rename https://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback

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

2 Comments

how can I rename image while copying?
if you want to rename while copying, just change destination file path name. fs.copyFile('source.txt', 'destination.txt', (err) => { if (err) throw err; console.log('source.txt was copied to destination.txt'); });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.