Using JS, this cannot be done. If your trying to download a file to specific destination in the drive it is not possible. This possess a huge security risk if browsers allow it.
When a user is viewing a website, they are using a browser to access the webpage and browser decides what all permissions a website can have and what limitations it should put.
The only way that you download a file in specific folder is,the user doing to himself. Otherwise downloads made by browser will be stored at some default destination like /user/Downloads or user chosen path /user/Downloads/Chrome_Downloads.
[Javascript} Can I specify the download directory when using the "download" function?
javascript - Download A File At Different Location Using HTML5 - Stack Overflow
Javascript download an on-the-fly generated BLOB file into specific folder - Stack Overflow
javascript - Download to a particular folder from a web application - Stack Overflow
In the example below, is there an option I can add to specify the download directory?
Currently the file saves to my (browser's) default download location (desktop).
But I want the file to save to the same folder as the index.html file.
Thank you.
<!doctype html>
<a href="" id="downlink" download="">Download me</a>
<script>
function download(file) {
let link = document.getElementById("downlink");
link.setAttribute("download", file);
link.click()
};
download("text.txt");
</script>source
It's not possible because this poses a security risk. People use fairly real information for their folder structure and accessing the folder names in itself poses an immediate risk. As described here:
Get browser download path with javascript
Most OSs tend to just default to a Download location and this is something the user decides through the Browser they use. Not the website.
In Chrome, Download location setting can be found at chrome://settings/downloads
This is now possible in most Chromium based desktop browsers (and safari soon), using the File System Access API. https://caniuse.com/native-filesystem-api
An example on how to do this can be found here: https://web.dev/file-system-access/#create-a-new-file
Something along the lines of:
async function getHandle() {
// set some options, like the suggested file name and the file type.
const options = {
suggestedName: 'HelloWorld.txt',
types: [
{
description: 'Text Files',
accept: {
'text/plain': ['.txt'],
},
},
],
};
// prompt the user for the location to save the file.
const handle = await window.showSaveFilePicker(options);
return handle
}
async function save(handle, text) {
// creates a writable, used to write data to the file.
const writable = await handle.createWritable();
// write a string to the writable.
await writable.write(text);
// close the writable and save all changes to disk. this will prompt the user for write permission to the file, if it's the first time.
await writable.close();
}
// calls the function to let the user pick a location.
const handle = getHandle();
// save data to this location as many times as you want. first time will ask the user for permission
save(handle, "hello");
save(handle, "Lorem ipsum...");
This will prompt the user with a save file picker where he can choose a location to save the file to. In the options, you can specify a suggested name and the file type of the file to be saved.
This will return a file handle, which can be used to write data to the file. Once you do this, the user is asked for write permission to the created file. If granted, your app can save data to the file as many times as you like, without re-prompting the user, until all tabs of your app are closed.
The next time your app is opened, the user is prompted for permission again, if you use the same file handle again (the handles can be saved in IndexedDB to persist them across page loads).
The File System Access API also allows you to let the user pick an existing file, for your app to save later. https://web.dev/file-system-access/#ask-the-user-to-pick-a-file-to-read
You will need to use the downloads api and add the "downloads" permission to the manifest.
browser.downloads.download({
url: URL.createObjectURL(new Blob([ textToSave ])),
filename: "test/test.txt",
saveAs: false,
})
If the filename is a path, any parent folders will be created if needed.
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/downloads/download
Subfolders require Firefox 51+ https://bugzilla.mozilla.org/show_bug.cgi?id=1280044
Have you tried a leading / or \ in front your file-destination (/VNW/..) or use a full file-destination (..\downloads\VNW\myfile.txt) Are you running this on your own server? Maybe you may need to explore your file system structure a little more??? You might need to capture the users chosen filepath from the browser?
I'm using the code offered in [this S.O. post] (https://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser) to allow the user to download state settings. (For reference, this is a music app, and they are downloading their "compositions.")
This function places the file into the "downloads" folder. Is it possible to allow the user to specify which folder to put the file? (Eg: for the reverse operation, <input type="file"/> will open a dialogue box whereby the user specifies the file to be read.)
It is not possible... fortunately! Imagine what would happen if JS which runs in your browser could change your filesystem. The security hole would be so big that everyone would (and definitely should) stop using the Internet. Imagine a situation where I've built a website which onload fires the code which save a file in your filesystem. The file lands in your cron.daily directory (suppose you use Linux). What is the file doing? - you may ask (if you knew that it's been even saved :smiling_imp:). Nothing special - just looking for some private data and when finished it deletes random files from /usr/bin, /proc, /sys and maybe /etc - just to see what happens.
Do you see the problem now? The code which runs in your browser before you can react to this cannot have such power to save anything in your filesystem. The only thing you can do to give the user a file is to use a module like Filesaver.js which, in fact, does not have access to user's file system at all. It just makes a GET request to the file directly and it's the browser which downloads the file (because that's how a browser works). So the only way you can change the location of the downloaded file is to change the browser settings. No other way I know of.
The answer is No, changing a directory is not possible due to security reasons in the File API.
https://github.com/eligrey/FileSaver.js/issues/42
JavaScript cannot exert any control over my (the visitor's) local filesystem. I remain in complete control of where my downloaded files go, what they are named, and indeed whether I even want to download them in the first place.
Sorry, but the best you can do is inform your users where to put the file you're offering for download. You cannot use JavaScript to choose the destination yourself.
You should be able to do this using a Java applet assuming that you have signed it. The user would be asked to allow your code to run and if allowed, you could do whatever you want: Including downloading a file to a specific location.
Nope, I'm fairly sure this is not possible using JavaScript on any browser.
The only thing you can suggest is the file's name.
Since you are developing an App for internal use, you might have some influence on the browser that is used. As of spring 2022, Chrome, Opera, Edge support the File-System-Access-API which let you show a openFilePickerDialog or a saveFilePickerDialog which are independent from the default download location or the last file-upload path of the browser.
Once you have selected a file from the desired folder, this location will be stored in your browser preferences (I guess) and the following calls will directly open to this folder.
Take a look at this answer for further reading.
As shown here, it is then possible to specify in which folder the "file-dialog" should start:
const fileHandle = await window.showOpenFilePicker({
startIn: 'pictures'
});
Instead of picturesyou can specify any "well-known" system folder, or if you program for a well known environment, any path that is likely to be found on the target machines.
I am wondering if it is possible to install files to a specific directory other than the downloads folder using JavaScript.
No. Only the end-user is allowed to specify a download location in the browser other than the default download location. This is a security feature so web-sites and their Javascript cannot put files in locations of their choice on your hard drive (e.g. replace executables, add startup files, etc...).
If I understand your question correctly, the answer is nope, this is not possible!
Use XMLHttpRequest() to request file from server as a Blob, utilize URL.createObjectURL(), <a> element with download attribute and href attribute set to Blob URL, then call .click() on <a> element.
var request = new XMLHttpRequest();
request.open("GET", "http://example.com/service/getUserImage/339/256");
request.responseType = "blob";
request.onload = function() {
var a = document.createElement("a");
a.href = URL.createObjectURL(this.response);
a.download = this.response.name;
document.body.appendChild(a);
a.click();
}
request.send();
try the following:
Create a hidden link when the ajax is complete with a attribute of download,trigger a click event on that link
$('body').append('<a class="hidden-ajax" download href="'+url+'"></a>')
$('.hidden-ajax').trigger('click');