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.
Related
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.
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).
I have a .desktop file and I want to read the icon png image file path in javascript so I can display it in my html file. How would I go about doing this?
This is what my .desktop file looks like:
[Desktop Entry]
Version=1.0
Name=BackMeUp
Comment=Back up your data with one click
Exec=/home/alex/Documents/backup.sh
Icon=/home/alex/Pictures/backup.png
Terminal=false
Type=Application
Categories=Utility;Application;
Any help would be greatly appreciated!
If the javascript you saying is only the javascript in browser, it's impossible except you upload the file to a server and analyze it.
1.browser javascript and server
First of all you should have a form that you can upload your .desktop file, and then you upload to a server(you can use node.js to build a server).
After the server received it, you can read its content as text and find the line of Icon and then response to browser
2. serverside javascript: node.js
if you can use node.js for reading this file, it's much easier.
just install node.js and build a .js file and use fs.readFileSync to read the .desktop file and analyze it line by line.
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.
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.