【问题标题】:Flowjs file upload - Ionic and ngCordovaFlowjs 文件上传 - Ionic 和 ngCordova
【发布时间】:2015-06-22 09:16:15
【问题描述】:

我正在尝试将图像从我的应用程序上传到服务器,我想使用 ngCordova 文件传输插件,但我对这个插件为您提供的进度信息不满意,所以我决定使用 Flowjs ,但我无法从我拥有的文件 url 创建一个正确的 HTML5 File 对象。 这是我的代码

window.resolveLocalFileSystemURL(photo, function(fileEntry){
    fileEntry.file(function(file){
        $scope.flow.addFile(file);
        $scope.flow.upload();
        $scope.flow.on('fileProgress', function (file, chunk){
            console.log(file);
            console.log(chunk);
        });
        $scope.flow.on('error', function(a,b){
            console.log(a);
            console.log(b);
        });
    });
    }, function(err){
        console.log(err);
});

其中 photo 是文件的文件系统的路径。 当我尝试上传时收到 400(错误请求)错误,我确信服务器端是正确的,因为我将它与许多其他 Flowjs 应用程序一起使用。 我认为 fileEntry.file() 返回的对象不是正确的 HTML5 文件对象,也许从文件 url 创建一个 Blob 可以解决问题,但我不明白如何创建它。 不过,在尝试创建 Blob 之前,我想让我的代码正常工作,但如果这是唯一的解决方案,那么......

【问题讨论】:

    标签: javascript ionic file-transfer ngcordova flow-js


    【解决方案1】:

    好的,我找到了一个可行的解决方案,不知道它是否是最好的,任何改进都非常感谢。

    window.resolveLocalFileSystemURL(photo, function(fileEntry){
        fileEntry.file(function(file){
            var reader = new FileReader();
            reader.onload = function(){
                var blob = new Blob([reader.result], {type: 'application/octet-stream'});
                blob.name = 'image.jpg';
                $scope.flow.addFile(blob);
                $scope.flow.upload();
                $scope.flow.on('fileProgress', function (file, chunk){
                    console.log(file);
                    console.log(chunk);
                });
                $scope.flow.on('error', function(a,b){
                    console.log(a);
                    console.log(b);
                });
            };
            reader.readAsArrayBuffer(file);
        });
    }, function(err){
        console.log(err);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多