javascript Cordova 使用文件 url 移动文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27910783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cordova Move File using the file url
提问by Scratch.
How can I move a file using the URL I get from the Camera?
如何使用从相机获取的 URL 移动文件?
neither successCallback nor errorCallback is called by the function moveTo. Can anyone tell me what I am doing wrong and what a possible solution looks like?
函数 moveTo 既不调用 successCallback 也不调用 errorCallback。谁能告诉我我做错了什么以及可能的解决方案是什么样的?
function successCallback(entry) {
console.log("New Path: " + entry.fullPath);
alert("Success. New Path: " + entry.fullPath);
}
function errorCallback(error) {
console.log("Error:" + error.code)
alert(error.code);
}
// fileUri = file:///emu/0/android/cache/something.jpg
function moveFile(fileUri) {
newFileUri = cordova.file.dataDirectory + "images/";
oldFileUri = fileUri;
fileExt = "." + oldFileUri.split('.').pop();
newFileName = guid("car") + fileExt;
// move the file to a new directory and rename it
fileUri.moveTo(cordova.file.dataDirectory, newFileName, successCallback, errorCallback);
}
I am using Cordova version 4.1.2 Also installed the Cordova File Plugin
我使用的是 Cordova 版本 4.1.2 还安装了 Cordova File Plugin
回答by QuickFix
You're trying to call the function moveTo on a String.
您正在尝试在字符串上调用函数 moveTo。
moveTOis not a function of String but of fileEntry. So first thing you need to do is get a fileEntry from your URI.
moveTO不是 String 的函数,而是 fileEntry 的函数。因此,您需要做的第一件事是从您的 URI 中获取一个 fileEntry。
For that you'll call window.resolveLocalFileSystemURL:
为此,您将调用window.resolveLocalFileSystemURL:
function moveFile(fileUri) {
window.resolveLocalFileSystemURL(
fileUri,
function(fileEntry){
newFileUri = cordova.file.dataDirectory + "images/";
oldFileUri = fileUri;
fileExt = "." + oldFileUri.split('.').pop();
newFileName = guid("car") + fileExt;
window.resolveLocalFileSystemURL(newFileUri,
function(dirEntry) {
// move the file to a new directory and rename it
fileEntry.moveTo(dirEntry, newFileName, successCallback, errorCallback);
},
errorCallback);
},
errorCallback);
}