Cordova:如何将文件移动到下载文件夹? [英] Cordova: How to move file to the Download folder?
问题描述
情况:
在我的移动应用中,我需要下载一个文件并将其存储在下载文件夹中.
下载部分工作正常.该文件已从服务器正确下载并存储在以下文件夹中:
file:///storage/emulated/0/Android/data/org.cordova.MY_APP_NAME.app/my_file.pdf
但该位置对用户并不友好.
要访问它,我必须转到:内部存储/Android/数据/org.cordova.MY_APP_NAME.app/
所以我需要将它移动到主下载文件夹.
文件传输是我无法做到的.
我知道已经有几个关于 SO 的类似问题.
我已经尝试了所有这些,但没有一个对我有用,我永远无法在实际的下载文件夹中看到该文件.
项目信息:
我正在使用 Quasar 和 Vuejs 和 Cordova.
平台:
目前我正在使用 Android.但理想情况下,我正在寻找适用于 Android 和 IOS 的解决方案.
代码:
下载代码:
var fileTransfer = new FileTransfer()//eslint-disable-linevar uri = encodeURI('https://MY_SERVER_PATH')文件传输.下载(尿,cordova.file.externalApplicationStorageDirectory + 'my_file.pdf',条目 =>{console.log('下载完成:' + entry.toURL())this.moveFile(entry.toURL())},错误 =>{console.log('下载错误源' + error.source)console.log('下载错误目标' + error.target)console.log('下载错误代码' + error.code)},错误的,{标题:{'授权':'基本asdasdasdasdassdasdasd'}})
文件传输代码:
moveFile(fileUri) {window.resolveLocalFileSystemURL(文件Uri,文件条目 =>{让 newFileUri = 'file:///storage/emulated/0/Download'window.resolveLocalFileSystemURL(新文件Uri,dirEntry =>{fileEntry.moveTo(dirEntry, 'new_filename.pdf', this.moveFileSuccess, this.moveFileError)},this.moveFileError)},this.moveFileError)},移动文件成功(条目){console.log('文件移动成功')控制台日志(条目)},移动文件错误(错误){console.log('文件移动错误')控制台日志(错误)}
问题:
如何将文件移动到下载文件夹?
谢谢
这是 cordova.file
对象的控制台日志:
applicationDirectory: "file:///android_asset/"applicationStorageDirectory: "file:///data/user/0/org.cordova.MY_APP_NAME.app/"cacheDirectory:"file:///data/user/0/org.cordova.MY_APP_NAME.app/cache/"数据目录:file:///data/user/0/org.cordova.MY_APP_NAME.app/files/"文件目录:nullexternalApplicationStorageDirectory: "file:///storage/emulated/0/Android/data/org.cordova.MY_APP_NAME.app/"externalCacheDirectory: "file:///storage/emulated/0/Android/data/org.cordova.MY_APP_NAME.app/cache/"externalDataDirectory: "file:///storage/emulated/0/Android/data/org.cordova.MY_APP_NAME.app/files/"externalRootDirectory: "file:///storage/emulated/0/"共享目录:空同步数据目录:空临时目录:空
好的,我设法解决了.
首先完全没有必要下载然后移动文件.它可以直接在所需的方向下载.
正确的路径(就我而言)是这样的:
cordova.file.externalRootDirectory + 'download/' + 'my_file.pdf
对应于:
file:///storage/emulated/0/download/my_file.pdf
这意味着要在设备内部找到文件,您必须转到:内部存储/下载/my_file.pdf
在 config.xml 中添加以下值:
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,root"/> 使用此cordova插件检查权限很重要:cordova-plugin-安卓权限
您可以像这样进行快速测试:
let permissions = cordova.plugins.permissions权限.检查权限(权限.READ_EXTERNAL_STORAGE,checkPermissionCallback,空)功能检查权限回调(状态){console.log('检查权限')控制台日志(状态)}
结果很可能是false
.这意味着我们必须向用户请求权限:
permissions.requestPermission(successCallback, errorCallback, permission)
这样就会出现请求权限的提示.
代码:
总之,这是工作代码:
let pdfPath = 'https://MY_SERVER_PATH'让权限=cordova.plugins.permissions权限.检查权限(权限.READ_EXTERNAL_STORAGE,checkPermissionCallback,空)//检查权限功能检查权限回调(状态){console.log('检查权限')控制台日志(状态)如果(!status.hasPermission){var errorCallback = 函数 () {console.warn('存储权限未开启')}//向用户请求权限permission.requestPermission(权限.READ_EXTERNAL_STORAGE,功能(状态){如果(!status.hasPermission){错误回调()} 别的 {//继续下载下载文件()}},错误回调)} 别的 {下载文件()}}函数下载文件(){让 filePath = cordova.file.externalRootDirectory + 'download/' + 'my_file.pdf'让 fileTransfer = new window.FileTransfer()让 uri = encodeURI(decodeURIComponent(pdfPath))//下载文件fileTransfer.download(uri, filePath,功能(入口){console.log('文件下载成功,完整路径为' + entry.fullPath)控制台日志(条目)},功能(错误){console.log('错误')控制台日志(错误)},错误的)}
THE SITUATION:
In my mobile app I need to download a file and store in the Download folder.
The download part is working fine. The file is properly downloaded from the server and stored in the following folder:
file:///storage/emulated/0/Android/data/org.cordova.MY_APP_NAME.app/my_file.pdf
But the location is not really user-friendly.
To access it I have to go to: Internal storage / Android / data / org.cordova.MY_APP_NAME.app /
So I need to move it to the main Download folder.
The file transfer is what I don't manage to do.
I know that there are already several similar questions on SO.
I have tried them all but none really worked for me, I could never see the file in the actual Download folder.
PROJECT INFO:
I am using Quasar with Vuejs and Cordova.
PLATFORM:
For the moment I am working with Android. But ideally I am looking for a solution that works for both Android and IOS.
THE CODE:
The download code:
var fileTransfer = new FileTransfer() // eslint-disable-line
var uri = encodeURI('https://MY_SERVER_PATH')
fileTransfer.download(
uri,
cordova.file.externalApplicationStorageDirectory + 'my_file.pdf',
entry => {
console.log('download complete: ' + entry.toURL())
this.moveFile(entry.toURL())
},
error => {
console.log('download error source ' + error.source)
console.log('download error target ' + error.target)
console.log('download error code' + error.code)
},
false,
{
headers: {
'Authorization': 'Basic asdasdasdasdassdasdasd'
}
}
)
The File transfer code:
moveFile(fileUri) {
window.resolveLocalFileSystemURL(
fileUri,
fileEntry => {
let newFileUri = 'file:///storage/emulated/0/Download'
window.resolveLocalFileSystemURL(
newFileUri,
dirEntry => {
fileEntry.moveTo(dirEntry, 'new_filename.pdf', this.moveFileSuccess, this.moveFileError)
},
this.moveFileError)
},
this.moveFileError)
},
moveFileSuccess(entry) {
console.log('file move success')
console.log(entry)
},
moveFileError(error) {
console.log('file move error')
console.log(error)
}
THE QUESTION:
How can I move a file to the Download folder?
Thanks
EDIT:
This is the console log of the cordova.file
object:
applicationDirectory: "file:///android_asset/"
applicationStorageDirectory: "file:///data/user/0/org.cordova.MY_APP_NAME.app/"
cacheDirectory:"file:///data/user/0/org.cordova.MY_APP_NAME.app/cache/"
dataDirectory: "file:///data/user/0/org.cordova.MY_APP_NAME.app/files/"
documentsDirectory: null
externalApplicationStorageDirectory: "file:///storage/emulated/0/Android/data/org.cordova.MY_APP_NAME.app/"
externalCacheDirectory: "file:///storage/emulated/0/Android/data/org.cordova.MY_APP_NAME.app/cache/"
externalDataDirectory: "file:///storage/emulated/0/Android/data/org.cordova.MY_APP_NAME.app/files/"
externalRootDirectory: "file:///storage/emulated/0/"
sharedDirectory: null
syncedDataDirectory: null
tempDirectory: null
Okay I managed to resolve it.
First of all is totally unnecessary to download and then move the file. It can just be directly downloaded in the desired direction.
The correct path (in my case) was this:
cordova.file.externalRootDirectory + 'download/' + 'my_file.pdf
that correspond to:
file:///storage/emulated/0/download/my_file.pdf
and that means that to find the file inside the device you have to go to: Internal Storage / Download / my_file.pdf
Add the following value in the config.xml:
<preference name="AndroidPersistentFileLocation" value="Compatibility" /> <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,root" />
It's important to check for permission using this cordova plugin: cordova-plugin-android-permissions
You can make a quick test like this:
let permissions = cordova.plugins.permissions
permissions.checkPermission(permissions.READ_EXTERNAL_STORAGE, checkPermissionCallback, null)
function checkPermissionCallback(status) {
console.log('checking permissions')
console.log(status)
}
Most probably the result is false
. And that means that we have to request permission to the user:
permissions.requestPermission(successCallback, errorCallback, permission)
In this way it will appear the alert asking for permission.
THE CODE:
To put it all together, this is the working code:
let pdfPath = 'https://MY_SERVER_PATH'
let permissions = cordova.plugins.permissions
permissions.checkPermission(permissions.READ_EXTERNAL_STORAGE, checkPermissionCallback, null)
// Checking for permissions
function checkPermissionCallback(status) {
console.log('checking permissions')
console.log(status)
if (!status.hasPermission) {
var errorCallback = function () {
console.warn('Storage permission is not turned on')
}
// Asking permission to the user
permissions.requestPermission(
permissions.READ_EXTERNAL_STORAGE,
function (status) {
if (!status.hasPermission) {
errorCallback()
} else {
// proceed with downloading
downloadFile()
}
},
errorCallback)
} else {
downloadFile()
}
}
function downloadFile() {
let filePath = cordova.file.externalRootDirectory + 'download/' + 'my_file.pdf'
let fileTransfer = new window.FileTransfer()
let uri = encodeURI(decodeURIComponent(pdfPath))
// Downloading the file
fileTransfer.download(uri, filePath,
function (entry) {
console.log('Successfully downloaded file, full path is ' + entry.fullPath)
console.log(entry)
},
function (error) {
console.log('error')
console.log(error)
},
false
)
}
这篇关于Cordova:如何将文件移动到下载文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!