【问题标题】:phonegap image download error code 3phonegap 图像下载错误代码 3
【发布时间】:2016-02-09 20:06:33
【问题描述】:


我正在尝试通过使用 phonegap 文件传输插件下载方法从 url 下载图像
但总是得到错误代码:3,
这是我的代码:

window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function(fs)
{
    // create the download directory is doesn't exist
    fs.root.getDirectory('download', { create: true });

    //  save file in .. download
    var filePath = fs.root.fullPath + 'download/' + url.split('/').pop();
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(decodeURIComponent("http://phonegap.com/uploads/artwork/Build-Bot-Preview.png"));
    filePath="file:///sdcard"+filePath;


    fileTransfer.download(uri,filePath,function(entry)
    {
        alert("Successfully downloaded file, full path is " + entry.fullPath);
    },
    function(error)
    {
        alert("Some error " + error.code + " for " + url);
    }, 
    false);
});

谢谢。

【问题讨论】:

    标签: android cordova download phonegap-plugins file-transfer


    【解决方案1】:

    我在一个新创建的 phonegap 项目中尝试了您的代码,它对我有用。我收到an alert 说它成功了。

    index.js:

    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    
        var url = "http://animalia-life.com/image.php?pic=/data_images/dog/dog1.jpg";
    
        window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function(fs)
        {
            // create the download directory is doesn't exist
            fs.root.getDirectory('download', { create: true });
    
            //  save file in .. download
            var filePath = fs.root.fullPath + 'download/' + url.split('/').pop();
            var fileTransfer = new FileTransfer();
            var uri = encodeURI(decodeURIComponent("http://phonegap.com/uploads/artwork/Build-Bot-Preview.png"));
            filePath="file:///sdcard"+filePath;
    
            fileTransfer.download(uri,filePath,function(entry)
            {
                alert("Successfully downloaded file, full path is " + entry.fullPath);
            },
            function(error)
            {
                alert("Some error " + error.code + " for " + url);
            }, 
            false);
        });
    
    },
    

    我希望它有所帮助。至少你知道代码现在可以工作了。

    编辑:

    您的 config.xml 在访问源行中有吗?:

    <access origin="*" />
    

    编辑:编辑:

    我在 config.xml 中用这些行声明插件:

    <gap:plugin name="cordova-plugin-file" source="npm" version="3.0.0" />
    <gap:plugin name="cordova-plugin-file-transfer" source="npm" version="1.3.0" />
    

    【讨论】:

    • 创建了新项目并添加了相同的代码仍然无法正常工作。相同的错误代码:3 在 android
    • 是的,我有,这是我的 config.xml &lt;content src="index.html" /&gt; &lt;access origin="*" /&gt; &lt;feature name="File"&gt; &lt;param name="android-package" value="org.apache.cordova.file.FileUtils" /&gt; &lt;param name="onload" value="true" /&gt; &lt;/feature&gt; &lt;feature name="FileTransfer"&gt; &lt;param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" /&gt; &lt;/feature&gt;
    • 我要问这个。您是否打开了设备上的 WiFi 连接?我还用你的 config.xml 替换了我的 config.xml,但我没有收到错误。顺便说一句,您遇到的错误是连接错误。
    • 是的,我在下载之前检查了我的连接,但仍然无法正常工作相同的错误代码:3
    • 当我访问 build.phonegap.com 并查看我使用您的配置创建的应用程序的插件选项卡时,我没有看到任何插件。
    猜你喜欢
    • 2015-07-06
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多