 |
|
BaselScript |
Beschreibung
Download file
▀ ▀ ▀ ▀ JAVA ▀ ▀ ▀ ▀
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
// Inside your activity or class function
public void downloadFile() {
String fileUrl = "http://your_server_url/download.php";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(fileUrl)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
// Handle the error
return;
}
File file = new File(getExternalFilesDir(null), "filename.ext");
try (ResponseBody responseBody = response.body();
FileOutputStream outputStream = new FileOutputStream(file)) {
if (responseBody != null) {
outputStream.write(responseBody.bytes());
}
// File downloaded successfully, you can now use the file.
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
▀ ▀ ▀ ▀ KOTLIN ▀ ▀ ▀ ▀
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.File
import java.io.FileOutputStream
// Inside your activity or fragment function
fun downloadFile() {
val fileUrl = "http://your_server_url/download.php"
val client = OkHttpClient()
val request = Request.Builder()
.url(fileUrl)
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
// Handle the error
return
}
val file = File(getExternalFilesDir(null), "filename.ext")
FileOutputStream(file).use { outputStream ->
outputStream.write(response.body?.bytes())
}
// File downloaded successfully, you can now use the file.
}
}
▀ ▀ ▀ ▀ PYTHON ▀ ▀ ▀ ▀
import requests
# URL of the PHP script that serves the file
php_script_url = `http://your_server_url/download.php`
# Define the local file path where you want to save the downloaded file
local_file_path = `downloaded_file.ext` # Change the file name and path as needed
# Send an HTTP GET request to the PHP script
response = requests.get(php_script_url)
if response.status_code == 200:
# Save the content of the response to the local file
with open(local_file_path, `wb`) as f:
f.write(response.content)
print(f"File downloaded to {local_file_path}")
else:
print(f"Failed to download file (HTTP Status Code: {response.status_code})")
▀ ▀ ▀ ▀ DELPHI ▀ ▀ ▀ ▀
uses
IdHTTP, IdSSL, IdSSLOpenSSL, SysUtils;
procedure DownloadFile(const AUrl, AFileName: string);
var
HTTP: TIdHTTP;
FileStream: TFileStream;
begin
HTTP := TIdHTTP.Create(nil);
try
# HTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
FileStream := TFileStream.Create(AFileName, fmCreate);
try
HTTP.Get(AUrl, FileStream);
finally
FileStream.Free;
end;
finally
HTTP.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
Url = `http://your_server_url/download.php`;
FileName = `C:PathToSavefilename.ext`; // Change the path as needed
begin
try
DownloadFile(Url, FileName);
ShowMessage(`File downloaded successfully.`);
except
on E: Exception do
ShowMessage(`Error downloading file: ` + E.Message);
end;
end;
▀ ▀ ▀ ▀ BASEL SCRIPT ▀ ▀ ▀ ▀
scene=1 name="download file"
section init
#url = "http://example.com/path_to_your_php/download.php"
download url= #url stream=csv directory= #_directory_temp file=docu_list delimiter=~~~
end
// download is ok
section download_done
message "download ok"
end
// download is bad
section download_error
message "error by download"
end
end scene 1