Open file on windows using nodeJS/AngularJS - javascript

I have a problem on opening files on windows.
These files (.pdf, .doc, .xlsx, .png, .....) are on the server computer: B where my REST application (node, Expres, angularJS) is deployed.
The client computer is rated A.
To open any file, I used this:
require ('child_process'). exec (' start '"' + pathOfFile);
Except that this opens the file on the computer B. And I want the file to be open on the computer A few be the way.
Someone would have any idea ?
Thank you

I can't add a comment (50 rep point).
If I understand you correctly you are trying to download a file that is on a REST server? if so, try using a GET method on your client and use File System library on your node application to actually load the file. like here - example.

Related

replace complete HTML file content from javascript

I have a html file under my war folder in the project ,
I just want to access that file From the path "/myhtml.html"
and then replace whatever is in this file from a new text "Some new Text"
so that My html file now will only have "some new text" there .
I searched alot for this , but they provide example for changing a specific div etc in a file , but i just want to replace everything in a file which is present under a war folder.
I understand that you are trying to change the content of HTML files using Javascript that is running in the browser.
Front-end Javascript can't access local files that are stored on your machine. It would be a security disaster if it could. The only way Javascript can read with local files is using a file upload input, and it will only be able to read the files not update/overwrite them.
NodeJS however is capable of accessing the local file system. NodeJs is backend JavaScript so it will be running on a server.

How can I get a list of files in a directory?

I'm trying to make a simple webpage that you can input a directory path in and it lists all the files in that directory. Is there a way I can do that directly in the .html file with js script? I have the path as a variable.
I'm very new to these programmin languages and don't know much about these.
A webpage does not have access to a client's file system. No website can see your files.
However, if you're talking about server side javascript, that's different. Server side javascript file listing can be achieved through this:
var fs = require('fs');
var files = fs.readdirSync('/assets/photos/');
Reference: Get list of filenames in folder with Javascript
Hope that helps
EDIT 1 (upload files with html):
You need to be more specific. But i will supose that you want to navigate into files for upload some file on your webpage:
You could do this and when you press the button you will can navigate in your files and select as you want:
<input type="file" name="" id="">
EDIT 2 (using server languages for working with files):
First step: you need a lenguage server as php or nodejs (it use internally javascript chromev8). I recomend you to use nodejs becose its easier for people who already know javascript.
If you wanna nodejs, install it:
https://nodejs.org/en/ .
If you wanna php you can install apache xampp that also install a version of mysql database:https://www.apachefriends.org/index.html
Second step: working with files:
php https://www.php.net/manual/en/intro.dio.php
nodejs: https://nodejs.org/api/fs.html#fs_file_system (you will need to know about synchronous and asynchronous programming).

Javascript to read Text File to Array without a Webserver or external libraries local on Windows PC

I have an HTML file with JavaScript that I am running without any Webserver/host so I am just opening the file in a browser local to my windows PC. In that HTML file I would like to be able to read a text file in the same folder as the html file. That file will contain data in rows and columns separated with tabs. i.e
1 a
2 b
3 c
I want to keep this as simple as possible so all I have to do is share the HTML and Text file to others so the can open it up local to their computer without any webserver/host and without having to also copy of external libraries like node.js or jquery.
I have searched and tested everything I can find but either I need to reference an external library or I have to run it in a webserver or I need to click a button to load the file through the browser, none of what I want.
Does native JavaScript support the function to read a text file and save it to an array? If so, any code direction would be great.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
XMLHttpRequest() exists in native JavaScript, I think it will help you.
You also can send a request to the file. Or use library: axios.js because when you use XMLHttpRequest() you lose many time to write code which just get content from file, with axios I got file content with one line: `axios.get('file.txt').then(result => console.log(result.data));
To connect Axios: <script src="https://unpkg.com/axios#0.18.0/dist/axios.min.js"></script>
You can read official documentation about axios.js and XMLHttpRequest() in the net.

Can I secure resources other than javascript in Nodewebkit?

I read this post
securing the source code in a node-webkit desktop application
I would like to secure my font files and I was thinking this snapshot approach might be a way. So instead of running this
nwsnapshot --extra-code application.js application.bin
Could I run
nwsnapshot --extra-code font_file font_file.bin
Then in package.json add this?
snapshot: 'font_file.bin'
Or would there be an alternative mechanism to reference the binary font? Would it be possible to convert the CSS file referencing the font into binary? Can anything else other than javascript be converted to binary?
One dumb thing you can do is to add your assets to the exe file as stated here:
https://github.com/nwjs/nw.js/wiki/How-to-package-and-distribute-your-apps#step-2a-put-your-app-with-nw-executable
Basically you have to create a zip of your content (included your package.json) and rename it to "package.nw" then you can "merge it" into the exe file by typing this if you're in windows (the link explains how to do this in other OS's):
`copy /b nw.exe+app.nw app.exe `
This is not a great security measure (beacuse it can be opened as a zip file) but is one step further.
Another thing that could add security to your files is to encrypt them and then add them dinamically through js (while decrypting them) for this you could use the encrypt and decrypt methods available in node.
http://lollyrock.com/articles/nodejs-encryption/
However the weakest point in the application is still the packages.json file and for this nw provides nothing.
Cheers!

Add content to .txt file from javascript

Is their a way- client side,
to add text to a file named text.txt in javascript?
In python:
f = open("text.txt","w")
f.write("Hello World")
f.close()
would write "Hello World" into the text file. I want to do something similar with javascript.
Note: I am running these files locally.
Its a bit more tricky than in python.
The problem is that the browser sandboxes your session and your possibilities.
It is possible to request storage space and read and write files in that space.
More info: http://www.html5rocks.com/en/tutorials/file/filesystem/
It would be a security nightmare if a javascipt file could just open files in the root or any other folder.
A different approach would be to upload the file in the client/ to the server edit it and send it back in a response.

Categories

Resources