我看了大部分帖子并找到了我修改过的以下代码以满足我的需求。但我似乎无法让它返回文件的全新路径。并且文件没有移动?
function successCallback(entry) {
alert("Success. New Path: " + entry.fullPath);
}
function errorCallback(error) {
console.log("Error:" + error.code)
alert(error.code);
}
function moveFile(fileUri) {
window.resolveLocalFileSystemURL(
fileUri,
function(fileEntry){
newFileUri = cordova.file.dataDirectory + "images/";
oldFileUri = fileUri;
fileExt = "." + oldFileUri.split('.').pop();
newFileName = uniqueId() + fileExt;
window.resolveLocalFileSystemURL(newFileUri ,
function(dirEntry) {
// move the file to a new directory and rename it
alert(dirEntry.fullPath)
fileEntry.moveTo(dirEntry, newFileName, successCallback, errorCallback);
},
errorCallback);
},
errorCallback);
}
现在调用 successCallback ,但新文件路径错误,文件未移动。
答案 0 :(得分:1)
我就这样做了
function moveFile(fileUri) {
window.resolveLocalFileSystemURI(fileUri, successCallback, errorCallback);
}
function successCallback(entry) {
var newFileName = uniqueId() + '.jpg';
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function (fileSys) {
fileSys.root.getDirectory("reunion", {
create: true,
exclusive: false
},
function (directory) {
window.rootFS = fileSys.root;
entry.moveTo(directory, newFileName, FileMoved, errorCallback);
}, errorCallback);
}, errorCallback);
}
function FileMoved(entry) {
var image = window.rootFS.toURL() + entry.fullPath;
var d = new Date();
var html = '<img class="imgPost" data-count="' + window.ImageCount + '" id="img' + window.ImageCount + '" width="100%" src="' + image + '?' + d.getTime() + '" />';
html += '<a id="deleteImgBnt' + window.ImageCount + '" href="#" data-count="' + window.ImageCount + '" class="deletephoto ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-d" data-role="button" data-theme="a" data-icon="delete" data-iconpos="right"> Delete Image </a> ';
$('#photo').prepend(html);
}
function errorCallback(error) {
console.log("Error:" + error.code)
alert(error.code);
}
function uniqueId() {
// desired length of Id
var idStrLen = 32;
// always start with a letter -- base 36 makes for a nice shortcut
var idStr = (Math.floor((Math.random() * 25)) + 10).toString(36) + "_";
// add a timestamp in milliseconds (base 36 again) as the base
idStr += (new Date()).getTime().toString(36) + "_";
// similar to above, complete the Id using random, alphanumeric characters
do {
idStr += (Math.floor((Math.random() * 35))).toString(36);
} while (idStr.length < idStrLen);
return (idStr);
}
function moveFile(fileUri) {
window.resolveLocalFileSystemURI(fileUri, successCallback, errorCallback);
}
function successCallback(entry) {
var newFileName = uniqueId() + '.jpg';
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function (fileSys) {
fileSys.root.getDirectory("reunion", {
create: true,
exclusive: false
},
function (directory) {
window.rootFS = fileSys.root;
entry.moveTo(directory, newFileName, FileMoved, errorCallback);
}, errorCallback);
}, errorCallback);
}
function FileMoved(entry) {
var image = window.rootFS.toURL() + entry.fullPath;
var d = new Date();
var html = '<img class="imgPost" data-count="' + window.ImageCount + '" id="img' + window.ImageCount + '" width="100%" src="' + image + '?' + d.getTime() + '" />';
html += '<a id="deleteImgBnt' + window.ImageCount + '" href="#" data-count="' + window.ImageCount + '" class="deletephoto ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-d" data-role="button" data-theme="a" data-icon="delete" data-iconpos="right"> Delete Image </a> ';
$('#photo').prepend(html);
}
function errorCallback(error) {
console.log("Error:" + error.code)
alert(error.code);
}
function uniqueId() {
// desired length of Id
var idStrLen = 32;
// always start with a letter -- base 36 makes for a nice shortcut
var idStr = (Math.floor((Math.random() * 25)) + 10).toString(36) + "_";
// add a timestamp in milliseconds (base 36 again) as the base
idStr += (new Date()).getTime().toString(36) + "_";
// similar to above, complete the Id using random, alphanumeric characters
do {
idStr += (Math.floor((Math.random() * 35))).toString(36);
} while (idStr.length < idStrLen);
return (idStr);
}