简体   繁体   English

是否可以允许用户指定要在PhoneGap中下载什么文件?

[英]Is it possible to allow the user to specify what file is to be downloaded in PhoneGap?

I'm developing an iPhone app (using JavaScript, HTML and PhoneGap/Cordova on Xcode) which on the click of a button downloads a file from dropbox and allows the user to read it into <p> tags. 我正在开发一个iPhone应用程序(在Xcode上使用JavaScript,HTML和PhoneGap / Cordova),单击按钮即可从保管箱下载文件,并允许用户将其读取到<p>标记中。 This all works but only when i specify the URL the programme is to follow to a specific file. 这一切都有效,但仅当我指定URL时,程序才能跟随特定文件。 I want the user to be able to chose their own file and download it to the app to be read. 我希望用户能够选择自己的文件并将其下载到应用程序中以供阅读。 The code i currently have is; 我目前拥有的代码是;

 function downloadfile() {
        var fileTransfer = new FileTransfer();
        var uri = encodeURI("http://dl.dropbox.com/u/97184921/readme.txt");

        fileTransfer.download(
                uri,
                '/Users/administrator/Library/Application Support/iPhone Simulator/6.1/Applications/AF96D141-0CE5-4D60-9FA8-8A8F9A999C81/Documents/readme.txt',
                function(entry) {
                    console.log("download complete: " + entry.fullPath);
                              alert("File Downloaded. Click 'Read Downloaded File' to see text");
                },
                function(error) {
                    console.log("download error source " + error.source);
                    console.log("download error target " + error.target);
                    console.log("upload error code" + error.code);
                }
        );
    }

    function readDownloadedFile() {
        myFileSystem.root.getFile("readme.txt", {create: false, exclusive: false}, gotDownloadFileEntry, fail);
        $('#mytext').load('http://dl.dropbox.com/u/97184921/readme.txt', function(){
                                                                                console.log("read is done");
                                                                            });
    }

    function gotDownloadFileEntry(fileEntry) {
        console.log(fileEntry);
        fileEntry.file(gotFile, fail);
    }

If it is possible to allow the user to specify the file to be downloaded, how could i alter the code to do so? 如果可以允许用户指定要下载的文件,我该如何更改代码呢? I'm fairly new to iPhone development so any help at all would be great :) 我对iPhone的开发还很陌生,所以任何帮助都很棒:)

This is the HTML for the buttons in case you need it; 这是按钮的HTML,以备不时之需。

    <button onclick="downloadfile();">Download Dropbox File</button>
    <button onclick="readDownloadedFile();">Read Downloaded File</button>

Use an input field to allow the users to specify the url they want to download. 使用输入字段允许用户指定他们要下载的URL。

<input type="text" id="toDl"/>

Then in your downloadFile() method you do: 然后在您的downloadFile()方法中执行以下操作:

var fileUrl = document.getElementById("toDl").value;

to get the url you want to download. 获取您要下载的网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM