PhoneGap的文件传输下载 [英] Phonegap Filetransfer download

查看:116
本文介绍了PhoneGap的文件传输下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在计算器新。这是第一次,我必须使用PhoneGap的,真的我有一个问题。我需要做一个表,并通过点击每个元素开始下载一个PDF文件,并​​创建一个新的文件夹(如果不存在的话)。但我不能甚至与PhoneGap的编译后,下载文件。我看到了所有的例子,刚刚经历的onload下载图像。

 <脚本类型=文/ JavaScript的>
功能downloadFile(){
 VAR URL =的http:// HTTP://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';
    VAR文件路径=本地/路径/到/你/文件';
    VAR文件传输=新的文件传输();
    VAR URI = EN codeURI(URL);
    fileTransfer.download(
        URI,
        文件路径,
        功能(进入){
            的console.log(下载完成:+ entry.fullPath);
        },
        功能(错误){
            的console.log(下载错误源+ error.source);
            的console.log(下载错误目标+ error.target);
            的console.log(上传错误code+错误code);
        },
        假,
        {
            标题:{
                授权:基本dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA ==
            }
        }
    );
}
< / SCRIPT>

HTML

 < TD的onclick =downloadFile()>行1 LT; / TD>


解决方案

您所有的URL首先是无​​效的HTTP包括多次

  VAR URL =的http:// HTTP://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';

改变这样的<​​/ P>

  VAR URL ='http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';

如果你是创建一个目录内的多个目录,存储文件,这可能会创建问题。(例如[项目/样品/本地]未创建同一时间。所以文件无法下载)。而确保文件下载插件都是可用config.xml和指定的PhoneGap版本。

使用此code。在一次创建多个目录。

  window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,gotFS,失败);//创建目录
功能gotFS(文件系统){
    window.FS =文件系统;
    VAR printDirPath =功能(输入){
        的console.log(目录路径 - + entry.fullPath);
    }    createDirectory(本地/路径/到/你的,printDirPath);
}
功能失效(){
    的console.log(未能获得文件系统);
}功能createDirectory(路径,成功){
    。VAR迪尔斯= path.split(/)反向();
    VAR根= window.FS.root;
    VAR createDir =功能(DIR){
        的console.log(创建目录+ DIR);
        root.getDirectory(DIR {
            创建:真实,
            独家:假的
        },successCB,failCB);
    };    VAR successCB =功能(输入){
        根=条目;
        如果(dirs.length大于0){
            createDir(dirs.pop());
        }其他{
            成功(输入);
        }
    };    VAR failCB =功能(){
    };    createDir(dirs.pop());
}

现在你写你的文件下载code。

I'm new on stackoverflow. it's the first time that I have to use Phonegap and really I have a problem. I need to make a table and by clicking on each element starts to download a pdf file and create a new folder (if it does not exist). But I can not even download a file after compiling with phonegap. All the examples I saw, just download an image through onload.

<script type="text/javascript">
function downloadFile(){
 var url = 'http://http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';
    var filePath = 'local/path/to/your/file';
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(url);
    fileTransfer.download(
        uri,
        filePath,
        function(entry) {
            console.log("download complete: " + entry.fullPath);
        },
        function(error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        },
        false,
        {
            headers: {
                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            }
        }
    );
}
</script>

HTML

<td onclick="downloadFile()">Row 1</td>

解决方案

First of all your url is invalid included http multiple times

var url = 'http://http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';

change like this

var url = 'http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';

If you are create multiple directory and store file inside that directory this may be create issue.(example [project/sample/local] not created same time. so file not download).And make sure file download plugins are available in config.xml and specify phonegap version.

Use this code to create multiple directory at time.

window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

// create directory
function gotFS(fileSystem) {
    window.FS = fileSystem;
    var printDirPath = function(entry){
        console.log("Dir path - " + entry.fullPath);
    }

    createDirectory("local/path/to/your", printDirPath);
}
function fail() {
    console.log("failed to get filesystem");
}

function createDirectory(path, success){
    var dirs = path.split("/").reverse();
    var root = window.FS.root;
    var createDir = function(dir){
        console.log("create dir " + dir);
        root.getDirectory(dir, {
            create : true,
            exclusive : false
        }, successCB, failCB);
    };

    var successCB = function(entry){
        root = entry;
        if(dirs.length > 0){
            createDir(dirs.pop());
        }else{
            success(entry);
        }
    };

    var failCB = function(){
    };

    createDir(dirs.pop());
}

Now you write your file download code .

这篇关于PhoneGap的文件传输下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆