【问题标题】:Phonegap download file from API not Server来自 API 而非服务器的 Phonegap 下载文件
【发布时间】:2015-09-17 21:46:42
【问题描述】:

我正在开发一个 Phonegap/Cordova 应用程序,我能够使用 FileTransfer 插件下载一个文件并将其放入手机的根文件夹中。该文件是托管在服务器上的简单图像。

但是,我正在尝试下载由我的 API 生成的文件(文件未托管在服务器上)。一旦你点击 api,它就会返回一个二进制文件流。此代码在浏览器中使用 AJAX 时运行良好,但在我的 Android 手机(测试)上不起作用。当我这样做时,我得到错误代码 1。

如果我的 URI 是托管在实际服务器上的文件,我的 fileTransfer.download 运行良好。

我返回文件的 API 是这种格式:

http://api.mydomainname.com/file/23

其中 23 是文件 ID。

这是我的代码:

document.addEventListener("deviceready", onDeviceReady, true);

var store;
var assetURL = encodeURI("http://api.mydomainname.com/file/23");
var fileName = "test.xlsx";

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotSys, fail);
}

function gotSys(fileSystem) {
    alert("Got FS");
    store = fileSystem.root;
    alert('Checking file: ' + store.toURL() + fileName);
    window.resolveLocalFileSystemURL(store.toURL() + fileName, appStart, downloadAsset);
}

function downloadAsset() {
    var fileTransfer = new FileTransfer();
    alert("About to start transfer");
    fileTransfer.download(assetURL, store.toURL() + fileName,
        function (entry) {
            alert("Success!");
        },
        function (err) {
            alert("Download error : " + err.code);
        });
}

function appStart() {
    alert('file exists');
    alert(store.toURL() + fileName);
}

function fail(error) {
    alert("fail error: " + error);
}

我尝试过的 C# 返回(尝试之一)是:

response = new HttpResponseMessage(HttpStatusCode.OK); 
response.Content = new StreamContent(new MemoryStream(docData)); 

response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");

response.Content.Headers.Add("Content-Type", "application/octet-stream");
response.Content.Headers.ContentDisposition.FileName = doc.DocumentName; 
return response;

我尝试返回二进制、json(带有文件二进制内容)等。有什么建议吗?甚至可以下载不在服务器上的文件吗?

谢谢

【问题讨论】:

    标签: cordova api download


    【解决方案1】:

    @尤拉,
    您可能有 white-list 插件问题。您需要将 white-list 插件添加到您的应用中。在此您还应该看到编译器和插件的版本。

    见:

    您还想阅读。
    Cordova/Phonegap 新开发人员的主要错误
    https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/new-to-Phonegap.md

    • 10 config.xml 中未添加新的“white-list”和“white-list plugin”参数。
    • 6 没有为您的编译器设置“phonegap 版本”
    • 7 没有为你的插件设置“版本”

    祝你好运,
    杰西

    【讨论】:

    • 谢谢。我在 config.xml 中添加了白名单权限,并将 URL 从 http 更改为 https,它似乎可以工作。我会做更多的测试,但谢谢你的建议。
    【解决方案2】:

    一个文本框,提交按钮和另一个有名称的文本框,点击提交按钮后,它应该下载一个文件作为文本文件。

    这是使用下面的 html 代码实现的...

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    form * {
      display: block;
      margin: 10px;
    }
    </style>
    <script language="Javascript" >
    function download(filename, text) {
      var element = document.createElement('a');
      element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
      element.setAttribute('download', filename);
    
      element.style.display = 'none';
      document.body.appendChild(element);
    
      element.click();
    
      document.body.removeChild(element);
    }
    </script>
    </head>
    <body>
    
    <form onsubmit="download(this['name'].value, this['text'].value)">
      <input type="text" name="name" value="test.txt">
      <textarea name="text">Please download the text and see it carefully. </textarea>
      <input type="submit" value="Download">
    </form>
    </body>
    </html>

    我希望在cordova phonegap 文件传输插件中实现这一点。

    这将帮助用户下载在那里创建的文件。

    试了很多地方都没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      相关资源
      最近更新 更多