【问题标题】:How to auto download file video in AngularJS如何在AngularJS中自动下载文件视频
【发布时间】:2018-03-16 09:40:49
【问题描述】:

我尝试在功能下载运行时自动下载视频。 我正在尝试使用 saveAs(fileSaver.js) 但出现错误

在“URL”上执行“createObjectURL”失败:找不到函数 与提供的签名相匹配。

任何人都可以提供帮助。

$scope.download = function(){
        var fd = new FormData();
        angular.forEach($scope.files, function(video){
            fd.append('video', video);
        });

        var request = $http({
            method : 'POST',
            url: xxxx + 'video/upload?start=' + $scope.start + '&end=' + $scope.end,
            data : fd,
            transformRequest:angular.identity,
            headers:{'Content-Type':undefined}
        });
        request.then(function(response){
            saveAs(response , 'video.mp4');
            $scope.isProcessing = false;
        }, function(error){
            $scope.isProcessing = false;
        });
    }

在服务端我使用烧瓶将返回如下:

return send_file(app.root_path + '/' + app.config['UPLOAD_FOLDER'] + '/uploadV/dd.mp4', as_attachment=True)

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    如果您使用的是 angular-file-saver,则需要使用 arraybuffer 响应类型来创建 blob:

    $scope.download = function(){
    
        var fd = new FormData();
        angular.forEach($scope.files, function(video){
            fd.append('video', video);
        });
    
        $http({
            method: 'POST',
            url: xxxx + 'video/upload?start=' + $scope.start + '&end=' + $scope.end,
            data: fd,
            transformRequest: angular.identity,
            responseType: 'arraybuffer',
            transformResponse: function (data) {
                var video;
                if (data) {
                    video = new Blob([data], {
                        type: 'video/mp4'
                    });
                }
                return {
                    response: video
                };
            },
        }).then(function(file){
            saveAs(file.response, 'video.mp4');
            $scope.isProcessing = false;
        }, function(error){
            $scope.isProcessing = false;
        });
    };
    

    【讨论】:

    • 我按照你的代码,我得到了错误。净::ERR_CONNECTION_ABORTED。我已经在我的模块中添加了 ngFileSaver。有其他方法上传您知道的视频吗? @lin
    • @HannahDelisa 你确定你的 API 端点和方法 POST 是可访问的 -> xxxx + 'video/upload?start=' + $scope.start + '&end=' + $scope.end 吗?
    • python 代码中的那个问题。现在每个人都认为很好,只有部分 saveAs(file.response, 'video.mp4'); .错误显示 TypeError:无法在新 FileSaver 中读取未定义的属性“类型”。感谢您的帮助@lin
    • 你知道除此之外的其他方法吗?
    • @HannahDelisa 你能把这个答案标记为正确吗? (投票答案按钮附近的灰色钩子)!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多