Not able to copy a file to a directory - javascript

I am copying a .jpg file to other directory(C:\myFaceApp\dropbox\exprtedFaces)
My source File : C:/myFaceApp/dropbox/faces/Monika1/1404039d-2be3-43bc-b20b-35c0f4a5954b/1404039d-2be3-43bc-b20b-35c0f4a5954b_00-00-04_crop.jpg
I am using following block of code to copy
targetPath=opts.exportDir; //C:\myFaceApp\dropbox\exprtedFaces
fs.createReadStream(req.query.facePath).pipe(fs.createWriteStream(targetPath));
res.write(JSON.stringify({ OK: 1 }));
res.end();
I am getting an error like this:
Error: EISDIR: illegal operation on a directory, open 'C:\myFaceApp\dropbox\exprtedFaces'

Your problem is that you are attempting to write to a directory not a file. createWriteStream takes a filename as it's argument. Try this instead:
fs.createReadStream(req.query.facePath).pipe(fs.createWriteStream(path.join(targetPath ,"file.jpg")));
You should ofc give it a non hard coded name, this is just an example. Have a look at the path module for that.

Related

node.js I need to be able to create a brand new JSON file

I am trying to create a new JSON file. Whenever I try to use the writeFile function, it just says there is no such directory open.
This is the code I've tried.
fs.writeFile('./UserData/' + msg.author + ".json", JSON.stringify({firstSplitContent: firstSplit,secondSplitContent: secondSplit,thirdSplitContent: thirdSplit,fourthSplitContent: fourthSplit,fifthSplitContent: fifthSplit},null,4), err => {
if(err) throw err;
console.log("File is created successfully.")
});
Error: ENOENT: no such file or directory, open './UserData/<#301910353967710208>.json'
help would be appreciated. Yes I know that windows wont use those special characters. I switched it to author.id to remove.
fs.writeFile won't create any directories that don't already exist.
I suspect that the directory in your path, UserData, doesn't exist, which is why you are getting that error.
Otherwise, your path may be wrong. Since you are using a relative path, the code will look for the UserData folder in the working directory, ie. where the code is currently executing.
If the path is correct, try creating it first:
if(!fs.existsSync('./UserData') {
fs.mkdirSync('./UserData')
}
Or, you could use a package such as fs-extra that gives you the capability of creating any folders in the path that don't already exist.
This is probably the most relevant function to you, if you want to use fs-extra:
https://github.com/jprichardson/node-fs-extra/blob/master/docs/outputJson.md
You should follow the answer of Willianm but just one thing I noticed: You are trying to use msg.author in your file name, but that's an object (a really big object), you might want to consider changing it to message.author.id

Unable to create a .html file with run time values in Javascript

I have below code which have to create html file with run time values.
var fs = require('fs');
const html = generateHtml(content); //Its successfully generated html content
fs.writeFileSysnc(buildPathHtml, html,(err) => {
if(err)
{
throw err;
console.log(err)
}
});
log.info('Succesfully created an HTML Report at ' + buildPathHtml);
I set the debug mode and I can see the entire .html structure that stored in html const in the above code and buildPathHtml is the location of the file.
But when I run the code and the below error is observed in output console. And the content is failed to write.
Output Console:
Error: ENOENT: no such file or directory, open 'c:\Users\arrchanaMohan\testCode\IncentRacktor\Test\shark\Reports\SANITY_2020-06-26:08:32:56.html'
Build Path:
c:\Users\arrchanaMohan\testCode\IncentRacktor\Test\shark\Reports\SANITY_2020-06-26:08:32:56.html
Appending the timestamp after 'SANITY_'
Kudos to #Teemu
I just removed colon (:) and used _ in the build path. And It worked.

file not found in node js (Intellij)

I am using node js express. I am trying to access a text file located in the same directory of that js file. So the file structure goes like this
- ProjectFolder
|
- many modules and folders
- routes
|
- Index.js
- input.txt
The Simple code that i have tried is ,
var data = fs.readFile('~/IdeaProjects/Title/routes/input.txt');
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
I did try different paths but nothing works. for your information, I am using fedora as os.
The error i got was,
Error: ENOENT: no such file or directory, open '~/IdeaProjects/Title/routes/input.txt'
at Error (native)
Any suggestion about how to access that file so that i can both read and write the contents of the file , will be welcomed. Looking for detailed answer.
Node doesn't interpret some characters that have special meaning like ~ or shell variables like $HOME, so you will need to use something like path.resolve() to get an absolute path or use a relative path (e.g. IdeaProjects/Title/routes/input.txt).
Also, as #Gothdo pointed out, there is a discrepency in the filename which will cause issues on case-sensitive file systems.
You will also need to either change fs.readFile() to fs.readFileSync() or add a callback to fs.readFile() like so:
fs.readFile('~/IdeaProjects/Title/routes/input.txt', function(err, data) {
if (err) throw err;
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
});

WinJs getFileAsync without stating a folder first

I have a string to the path of a file, and i would like to get the file. So far i have only found this, which relies on the file being within a specified folder, or a child of this folder.
var rootFolder = Windows.Storage.ApplicationData.current.localFolder;
rootFolder.getFileAsync('MY_FILE_PATH_FROM_ROOT').then(function (file) {
});
By rootFolder points to a folder where the app is installed, something like C:\Program Files....
What if i have a filePath string of something like C:\MyFiles\Picture\Pic_123.png. How would i get this file?
Sorry i am new to WinJs.
Kind of simple once you see the API. Use
Windows.Storage.StorageFile.getFileFromPathAsync('YOUR_FILE_PATH')
.then(function (file) {
});

Path to file won't work

I am currently trying to get a path to a file to work but it just won't let me.
I'm working on a virtual directory so the path will be dynamic.
this is how my Directories are set up:
WebServices
->LiveScanServ.asmx(this is the file I want)
LiveScan
->ScanFolders.aspx
My browser Url looks like :http://localhost:43234/dynamicPart/Home.aspx#
inside my ScanFolders.aspx I am making a call to the file LivescanServ.asmx however it just won't find it. This is what I have so far:
<Services><asp:ServiceReference Path="~/WebServices/LiveScanService.asmx" /></Services>
but when I run it, it gives me a 404 error(Not Found).
Any ideas?
edit: this is my javascript for calling ScanFolders.aspx:
function loadLiveScanSync() {
$('#centreMenu').slideUp('slow', function () {
$('#centreMenu').children('div').css('display', 'none');
$('#loadedContentHolder').load('LiveScan/ScanFolders.aspx');
$('#loadedContentHolder').css('display', 'block');
The file you want is LiveScanServ.asmx. The file you have in your service reference is LiveScanService.asmx. Make sure you can manually resolve your asmx file in the browser, and that the url matches the path in your config.
WebService ->LiveScanServ.asmx(this is the file I want)
<Services><asp:ServiceReference Path="~/WebServices/LiveScanService.asmx" /></Services>

Categories

Resources