Sources
For a JavaScript solution for either client-side or server-side, you can use Node.js; however,
- client side requires a package called: "NWJS" available here: http://nwjs.io/
- server side only requires "Node.js" available here: https://nodejs.org
You can find extensive documentation on either of these "JavaScript" solutions; however, there are other "JavaScript" solutions available, NodeJS is very popular.
If you work with another language on the server, like PHP, you can find more info about it here: http://php.net
Solution
The following describes a JavaScript solution with code for server-side that you can just copy & paste and modify to your needs.
This assumes you are running NodeJs on Linux and that the file/folder (path) is not recursive. The example below is not tested, feel free to test & fix as necessary.
For the client-side code interacting with the "server-side" example below, create an HTML form that uses: method="PUT" and the fields as required by the vars; -OR- use an AJAX method to accomplish the same.
Server-side: NodeJS
let http = require('http');
//File System package...
let fsys = require('fs');
let makePath = function(root, path, data)
{
try
{
fsys.accessSync(root, fsys.W_OK);
}
catch(err)
{
return {code:403, text:'Forbidden'}
}
path = ((path[0] == '/') ? path.substr(1, path.length) : path);
if (path.split('/').length > 2)
{ return {code:412, text:'Precondition Failed'}; }
if (fsys.existsSync(path))
{ return {code:409, text:'Conflict'}; }
if (path[path.length -1] == '/')
{ fsys.mkdirSync(root +'/'+ path.substr(0, path.length -2)); }
else
{ fsys.writeFileSync((root +'/'+ path), (data || ' '), 'utf8'); }
return {code:200, text:'OK'};
};
http.createServer
(
function(request, response)
{
let vars = url.parse(request.url);
if (path && (path.indexOf('/') > -1) && (request.method == 'PUT'))
{
var resp = makePath(__dirname, vars.path, vars.data);
response.statusCode = resp.code;
response.setHeader('Content-Type', 'text/plain');
response.end(resp.text);
}
}
).listen(8124);
Usage
You can access this from your web browser, if your server runs on the same machine, in your web-browser's address bar type: http://127.0.0.1:8124 and hit enter/return; however, please see the proper NodeJS documentation for serving the necessary client-side HTML & JavaScript as mentioned.
Had the exact same issue today, so I'll provide an answer in TypeScript(3.7.5) based on what worked out for me.
Tips:
- FileAPI needs a Blob to work with (as others have also stated), it won't work with a file path.
The function:
static async createFile(path: string, name: string, type: string): Promise<File> {
let response = await fetch(path);
let data = await response.blob();
let metadata = {
type: type
};
return new File([data], name, metadata);
}
The call:
await createFile('../assets/images/someInterestingPNGImage.png', 'iAmAFile.png', 'image/png')
.then((file) => {
//do something with ur file.
console.log(file);
});
File API needs a Blob here is work-arround
var GetFileBlobUsingURL = function (url, convertBlob) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = "blob";
xhr.addEventListener('load', function() {
convertBlob(xhr.response);
});
xhr.send();
};
var blobToFile = function (blob, name) {
blob.lastModifiedDate = new Date();
blob.name = name;
return blob;
};
var GetFileObjectFromURL = function(filePathOrUrl, convertBlob) {
GetFileBlobUsingURL(filePathOrUrl, function (blob) {
convertBlob(blobToFile(blob, 'testFile.jpg'));
});
};
var FileURL="test/test.jpg"
GetFileObjectFromURL(FileURL, function (fileObject) {
console.log(fileObject);
});
with javascript alone this move will create a security problem and I don't think it's possible to do. But on server side with some tool like Node.js you can by doing something like:
var fs = require("fs");
fs.mkdir("<your path>",callback);
manipulating client file with your js code often create security issues
No you can't do this using native Javascript.Native Javascript won't allow you to do any I/O in browser. But if its your necessary requirement then I would suggest you to use any server side tool like node.js. How to do in node.js?,You can get reference from @Moussa answer.
There is a library available to handle this, fswin:
https://www.npmjs.org/package/fswin
See the documentation for setAttribute here:
https://github.com/xxoo/node-fswin/wiki/setAttributes-and-setAttributesSync
In other words:
fswin.setAttributesSync('test.txt', { IS_HIDDEN: true });
Note that this requires a native compiler (it lists Visual Studio in the documentation, but perhaps others could be used).
You could try executing the DOS command attrib using child_process.spawn().