无法在适用于iOS的PhoneGap中获取文件路径

时间:2015-06-30 01:55:46

标签: jquery ios cordova phonegap-plugins filepath

我正在创建一个文件,我希望通过de.appplant.cordova.plugin.email-composer将其附加到电子邮件中。虽然电子邮件编辑器显示,并包含指示它已附加的文件图标,但控制台显示:

2015-06-29 21:21:16.473 bcr[8696:1803] File not found: cdvfile://localhost/persistent/export.csv

以下是我的代码:

//touch event
$(document).ready(function(e) {
    $("#shareBtn").on('touchstart', function (){
       var dummyStore = 'dummyStore';
       stringSuccess(dummyStore);
    });
})

//initiate storing
function stringSuccess(csv){
    DATA2FILE('export.csv',csv,fileSavedSuccess)
}

//open email composer and attach file
function fileSavedSuccess(file){
    console.log(file);
    FILE2EMAIL([],[],[],"QR Scan","Attached CSV file includes results of QR Scan.",file.localURL)
}

//file handling
function DATA2FILE (filename, data, callback) {

    var defaultFileName =  'export-file.txt';

    if (filename === undefined || filename === null) {
        filename = defaultFileName;
    }
    // Request the file system
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

    // Access to filesystem is OK
    function gotFS(fileSystem) {
        fileSystem.root.getFile(filename, {create: true}, gotFileEntry, fail);
    }

    // File is ready
    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
    }

    // Write file content
    function gotFileWriter(writer) {
        writer.onwriteend = function(evt) {
            console.log('finished writing');
            if (callback !== undefined) {
                callback(writer);
            }
        };
        writer.write(data);
    }

    function fail(error) {
        console.log('Error: ', error.code);
    }
}

var FILE2EMAIL =  function (recipient, cc, bcc, subject, body, attachementURL) 
{
    window.plugin.email.open({
        to:recipient, // contains all the email addresses for TO field
        cc:cc, // contains all the email addresses for CC field
        bcc:bcc, // contains all the email addresses for BCC field
        attachments: attachementURL, // contains all full paths to the files you want to attach
        subject:subject, // represents the subject of the email
        body:body, // represents the email body (could be HTML code, in this case set isHtml to true)
        isHtml:true // indicats if the body is HTML or plain text
        });
}

fileSavedSuccess(file)中将文件内容打印到控制台时的输出是

2015-06-29 22:02:05.781 bcr[8714:60b] {"fileName":"","length":10,"localURL":"cdvfile://localhost/persistent/export.csv","position":10,"readyState":2,"result":null,"error":null,"onwritestart":null,"onprogress":null,"onwrite":null,"onabort":null,"onerror":null}

但是,在将localURL值传递给FILE2EMAIL时出现File Not Found错误。路径错了吗?错误在哪里?

1 个答案:

答案 0 :(得分:0)

我使用fileSystem.root.toURL()开始工作。我能够通过更改' fileSavedSuccess'来检索正确的路径并访问我的文件内容。如下:

function fileSavedSuccess(file){
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
        url = fileSystem.root.toURL()+"export.csv";
        FILE2EMAIL('test@test.com,[],[],'QR Scan','See attached file.',url);
                }, function(){
                    alert("fails!");
                });
}