我正在尝试将带有PhoneGap的Progress事件用于文件传输。
我的问题是每次按下载按钮时,都会出现以下错误。
我在此链接下找到了代码并完全复制了它:http://www.raymondcamden.com/2013/05/01/Using-the-Progress-event-in-PhoneGap-file-transfers
错误: 10-23 23:55:38.407 24464-24508 /? E / FileTransfer:{“target”:“// download.mp3”,“http_status”:200,“code”:1,“source”:“http://archive.org/download/Kansas_Joe_Memphis_Minnie-When_Levee_Breaks/Kansas_Joe_and_Memphis_Minnie- When_the_Levee_Breaks.mp3“,”exception“:”/ download.mp3:open failed:EROFS(只读文件系统)“} java.io.FileNotFoundException:/download.mp3:open failed:EROFS(只读文件系统) 在libcore.io.IoBridge.open(IoBridge.java:409) 在java.io.FileOutputStream。(FileOutputStream.java:88) 在org.apache.cordova.CordovaResourceApi.openOutputStream(CordovaResourceApi.java:329) 在org.apache.cordova.CordovaResourceApi.openOutputStream(CordovaResourceApi.java:310) 在org.apache.cordova.filetransfer.FileTransfer $ 4.run(FileTransfer.java:894) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:841) 引起:libcore.io.ErrnoException:open failed:EROFS(只读文件系统) at libcore.io.Posix.open(Native Method) 在libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 在libcore.io.IoBridge.open(IoBridge.java:393) 在java.io.FileOutputStream。(FileOutputStream.java:88) 在org.apache.cordova.CordovaResourceApi.openOutputStream(CordovaResourceApi.java:329) 在org.apache.cordova.CordovaResourceApi.openOutputStream(CordovaResourceApi.java:310) 在org.apache.cordova.filetransfer.FileTransfer $ 4.run(FileTransfer.java:894) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:841)
的index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name = "format-detection" content = "telephone=no"/>
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>FileTransfer Test</title>
</head>
<body>
<div id="content">
<p>
Kansas Joe McCoy and Memphis Minnie – "When The Levee Breaks"
</p>
<img src="img/KansasJoeAndMemphisMinnie.jpg">
<p>
<button id="startDl">Click to Download and Play</button>
</p>
</div>
<div id="status"></div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>
app.js
document.addEventListener('deviceready', deviceready, false);
var buttomDom;
var statusDom;
var fileSystem;
function deviceready() {
console.log('dv ready');
//step one is to request a file system
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0,
function(fs) {
fileSystem = fs;
buttonDom = document.querySelector('#startDl');
buttonDom.addEventListener('touchend', startDl, false);
buttonDom.removeAttribute("disabled");
statusDom = document.querySelector('#status');
}, function(e) {
alert('failed to get fs');
alert(JSON.stringify(e));
});
}
function startDl() {
buttonDom.setAttribute("disabled","disabled");
var ft = new FileTransfer();
var uri = encodeURI("http://archive.org/download/Kansas_Joe_Memphis_Minnie-When_Levee_Breaks/Kansas_Joe_and_Memphis_Minnie-When_the_Levee_Breaks.mp3");
var downloadPath = fileSystem.root.fullPath + "/download.mp3";
ft.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
var perc = Math.floor(progressEvent.loaded / progressEvent.total * 100);
statusDom.innerHTML = perc + "% loaded...";
} else {
if(statusDom.innerHTML == "") {
statusDom.innerHTML = "Loading";
} else {
statusDom.innerHTML += ".";
}
}
};
ft.download(uri, downloadPath,
function(entry) {
statusDom.innerHTML = "";
var media = new Media(entry.fullPath, null, function(e) { alert(JSON.stringify(e));});
media.play();
},
function(error) {
alert('Crap something went wrong...');
});
}
答案 0 :(得分:1)
我找到了一个解决方案,我将完整路径更改为root.toURL()。