gpt4 book ai didi

php - 如何使用phonegap(Android平台)从php页面下载文件?

转载 作者:行者123 更新时间:2023-11-29 16:05:59 24 4
gpt4 key购买 nike

我正在使用 phonegap 创建一个 Android 应用程序,在这个应用程序中我想允许用户从 php 页面(服务器端)下载文件,这就是我遇到麻烦的地方。这是我的 Android 项目中的 html 页面 index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download</title>
</head>

<body>

<form id="down" name="down" action="http://172.25.10.99/test/download.php" method="GET">
<!-- 172.25.10.99 : server IP -->
<input type="text" name="filename" id="filename"/>
<input type="submit" id="id2" value="download"/>
</form>
</body>
</html>

这是 php 页面 download.php :

        <?php

try {
$file = "D:\\file\\" . $_REQUEST['filename'];
} catch (Exception $ex) {
$file = "D:\\file\\pdf2.pdf";
}
$fp = fopen($file, 'r');
$content = fread($fp, filesize($file));
fclose($fp);
header("Accept-Ranges: bytes");
header("Keep-Alive: timeout=15, max=100");
header("Content-Disposition: attachment; filename=" . basename($file));
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Description: File Transfer");

?>

问题是:当我在我的应用程序中按下下载按钮时(在插入一个正确且存在于服务器上的文件名之后),我什么也没得到,而我期待看到一个询问我的下载窗口在哪里保存请求的文件,或者更好,在自动下载的下载文件夹中找到请求的文件,我相信问题出在 php 页面的标题中。你能帮忙吗?我真的很感激..

最佳答案

使用FileTransfer.download,这里是一个例子:

function downloadFile(){

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry) {
var sPath = fileEntry.fullPath.replace("dummy.html","");
var fileTransfer = new FileTransfer();
fileEntry.remove();

fileTransfer.download(
"http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf",
sPath + "theFile.pdf",
function(theFile) {
console.log("download complete: " + theFile.toURI());
showLink(theFile.toURI());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
}
);
}, fail);
}, fail);
};

关于php - 如何使用phonegap(Android平台)从php页面下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18413595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com