无法在 iOS 的 PhoneGap 中获取文件路径
Posted
技术标签:
【中文标题】无法在 iOS 的 PhoneGap 中获取文件路径【英文标题】:Can't Get File Path in PhoneGap for iOS 【发布时间】:2015-06-30 01:55:46 【问题描述】:我正在创建一个文件,我想通过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】:我使用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!");
);
【讨论】:
以上是关于无法在 iOS 的 PhoneGap 中获取文件路径的主要内容,如果未能解决你的问题,请参考以下文章
在Phonegap的iOS中通过fileTransfer.download下载文件时无法设置元数据
如何使用 iOS 的 PhoneGap 获取 .ipa 文件?