且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将文件移动到应用程序目录cordova

更新时间:2022-10-16 10:35:39

I do think that the app/www directory is read-only since it contains your actual code and is packaged as .apk. Why would you need it there though? It is still since it is on disk. If you want to copy it for your own applications data partition, you should use the File plugin that you already mentioned to move it under cordova.file.externalDataDirectory or some other place you wish to move it.

Update

Since you want to show the file as image on HTML, the easiest way is to read it as Data URL (base64 format) and give that as src for image instead of path for file like this

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, onFail);}

function onFail(message) {
    alert('Failed because: ' + message);
}
function gotFS(fileSystem) {
    fileSystem.root.getFile("<your_image_file>.jpg", null, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.file(gotFile, fail);
}

function gotFile(file){
    readDataUrl(file);
}

function readDataUrl(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("Read as data URL");
        console.log(evt.target.result);

        var imageData = evt.target.result;
        document.getElementById("img#image1").src = imageData;
    };  
    reader.readAsDataURL(file); // Read as Data URL (base64)
}

The structure for that code is borrowed from the Pratik Sharma's answer and it may not completely work for your case but you should be able to pick up the idea from there. It relies on that you have this kind of image tag available on your HTML.

<img id="image1" src="" />

If you want to read more about this, consult this Wikipedia article.

上一篇 : :如何删除应用程序目录中的文件?下一篇 : IIS7中Web应用程序文件夹的IP限制

相关阅读

技术问答最新文章