php - 使用 PHP 从服务器下载离子 5 电容器文件
问题描述
我坚持在离子电容器中下载文件/附件。
为此,我使用了以下方法但不起作用,任何人都可以建议我吗?
第一种方法:-
download(fileURL: any, filename: any) {
this.platform.ready().then(() => {
const fileTransfer: FileTransferObject = this.transfer.create();
let url = encodeURI(fileURL); // *PHP FILE PATH download.php*
let path = null;
// //Check plateform
if (this.platform.is('ios')) {
path = this.file.documentsDirectory;
}
else {
path = this.file.externalDataDirectory;
}
// //Process download
fileTransfer.download(url, path + filename, false).then((entry) => {
console.log('download complete: ' + entry.toURL());
}, (error) => {
console.log('download EROR: ' + JSON.stringify(error));
});
});
}
**
结果:- 错误,错误代码 3 状态 401
**
第二种方法:-
download(fileURL: any, filename: any) {
this.platform.ready().then(() => {
let url = encodeURI(fileURL);// *PHP FILE PATH download.php*
let path = null;
// //Check plateform
if (this.platform.is('ios')) {
path = this.file.documentsDirectory;
}
else {
path = this.file.externalDataDirectory;
}
this.http.get(url, { responseType: "arraybuffer" }).subscribe(
(imageBlob: any) => {
console.log('Blob' + imageBlob);
return this.file.writeFile(path, filename, imageBlob, { replace: true, append: false }
).then(file => { console.log(JSON.stringify(file)); })
.catch(err => {
alert(JSON.stringify("Error 1: " + err));
});
}, error => {
alert(JSON.stringify("Error 2: " + error));
console.log(error);
}
);
});
}
结果:- 没有错误,但文件没有保存在设备中。
**
模拟器结果:- I/Capacitor/Console:文件:http://localhost/main.js
- 第 181 行 - 消息:{"isFile":true,"isDirectory":false,"name":"favicon__1_16244547901624879637.png","fullPath":"/favicon__1_16244547901624879637.png","filesystem":"<FileSystem: files>" "nativeURL":"file:///data/user/0/com.ionic.app/files/favicon__1_16244547901624879637.png"}
**
下载.php 文件:-
//used for download attachment
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: *');
header("Access-Control-Allow-Methods: GET");
$varFile = $_GET['file'];
$varDir = ($_GET['dir'] != '') ? $_GET['dir'] : '';
$ImagePath = $arrConfig['sourceRoot'] . $varDir . $varFile;
if (file_exists($ImagePath)) {
//Download function starts from here
$varFilePath = SOURCE_ROOT . $varDir . $varFile;
header("Pragma: public");
header("Expires: 0");
header("Content-Description: File Transfer");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=" . $varFile);
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($varFilePath));
@readfile($varFilePath);
exit();
}
解决方案
推荐阅读
- laravel - 为什么在laravel中创建帖子时标签和类别按钮的内容不可见?
- python - 如何循环此 Python PIL 代码以重复使用不同的图像?
- javascript - nodejs http2 服务器 addSession 事件使用不同的 sessionID 触发两次
- javascript - 无法将下拉列表中的值传递给 componentDidMount 函数
- r - 如何创建一个 facet_wrap 图,显示基于 R 中的组找到的前 10 个常用词?
- javascript - GMAIL API NodeJs
- c# - 希望将复杂的 json 模式转换为 C# 类,但是 API 返回的类型并不总是匹配
- python - 使用 Selenium 有时我的索引超出范围,而其他时候,没有 - PYTHON
- database - 如何设置适当的 MongoDB 副本集备份和警报系统
- python - 我应该在 Airflow 中使用 Python 原生多线程还是多任务?