Is there an easy way to download a text file from a website and save it upon download?
Not as a web app. You can do this with Cordova plugins if you build with VoltBuilder, or with Electron.
Letting web pages load files off the web and save them to your system is a very bad idea, from a security viewpoint.
You can use a standard Javascript approach to download a file to the device:
function download(content, filename, contentType){
if(!contentType) contentType = ‘application/octet-stream’;
var a = document.createElement(‘a’);
var blob = new Blob([content], {‘type’:contentType});
a.href = window.URL.createObjectURL(blob);
a.download = filename;
a.click();
}
This will act just like clicking a download link in the browser--the file will go the the standard downloads folder.
Getting the file is a little more complex: the browser may enforce "CORS" policies that are hard to work around.
The basic function is
async function getFile(url){
var response=await fetch(url)
var text=await response.text();
var name="insert the file name here)
download(name, text, 'text/html')
}
But the CORS rules may block this.
Thank you, it looks good - but:
I get a File, but is not inside the Text from file on the web.
Its only the Url from the file in the recived File: https://www.Website/File.ini
I call from basic like this:
Dim FileURL = “https://www.Website/File.ini”
Dim FileName = “File.ini”
Download(FileURL, FileName, “text/csv”)
Your Code is in Codefile Java.
But that I make as Remark:
// If(!contentType) contentType = ‘application/octet-stream’;
It gives an error without remark.
Also i must write async:
async function Download(content, filename, contentType)
What is wrong (i don’t konw)?
The JavaScript function name is “download” with a lower-case “d”. Javascript is sensitive to case and does not see this as the same function as “Download”
You need to change the name in one of these places
I’m trying it, but same result.
Here’s the call in the Basix section:
Dim FileURL = "https://www.ESD-Systeme.de/DLwww/EaSprachen.ini"
Dim FileName = "EaSprachen.ini"
filedownload(FileURL, FileName, "application/octet-stream")
So, currently I have the function in the Java area:
async function filedownload(content, filename, contentType)
{
//If(!contentType) contentType = 'application/octet-stream';
contentType = 'application/octet-stream';
//contentType = 'text/csv' // that was an test
var a = document.createElement('a');
var blob = new Blob([content], {'type':contentType});
a.href = window.URL.createObjectURL(blob);
a.download = filename;
a.click();
}
I get the correct filename with this content:
https://www.ESD-Systeme.de/DLwww/EaSprachen.ini
But the file im web contains the following:
EappSpraStand 3.4.2026
EaS0, Deutsch
EaS1, English
EaS2, Rumänisch
Sz, 2, HeaderEingabe, 0, ERNTEapp Erfassen & Speichern
Sz, 2, HeaderEingabe, 1, ERNTEapp Capture and Store
Sz, 2, HeaderEingabe, 2, ERNTEapp Capturați și stocați
...
You have an Idea?
I think you have misunderstood. The function that gets the file info from the web is “getFile” copied again below. It will also save the file if you replace the “insert_file_name_here” wiht the fle name you want.
To do this, your javascript code file must also contain the code for function download, which this function calls.
Unfortunately, that’s not possible either; it seems the server is now preventing it.
Access to fetch at 'https://www.esd-systeme.de/DLwww/EaSprachen.txt' from origin 'http://127.0.0.1:59538' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.Understand this error
The FetchEvent for "https://www.esd-systeme.de/DLwww/EaSprachen.txt" resulted in a network error response: the promise was rejected.
Yes. This is CORS policy problem. For a text file, there is fancier version of “fetch” that usually solves it, but I don’t have an example to hand. I’ll try to find an example tonight.