2014-10-08 43 views
-1

我已经使用aws javascript sdk编写了下载上传到amazon s3的视频的代码。一切正常,但一些视频在浏览器中打开并开始播放。这是下面的代码:如何强制从Amazon S3下载AngularJs中的视频?

查看:

<a href="#" ng-click="downloadVideo(video)">Download Video</a> 

控制器:

$scope.downloadVideo = function (video) { 
      videoLocation = video.video_location; 
      var bucketPath = videoLocation.substring(0, videoLocation.lastIndexOf("/") + 1); 
      bucketPath = bucketPath.substring(0, bucketPath.length - 1); 
      var fileName = videoLocation.substring(videoLocation.lastIndexOf("/") + 1, videoLocation.length); 
      var videoSignedUrl = VideoFactory.downloadVideo(bucketPath,fileName);    
      $window.open(videoSignedUrl); 
     } 

VideoFactory:

downloadVideo: function (bucketPath,fileName) { 
    bucketName = aws.bucket_name; 

    options = { 
     accessKeyId : 'XXXXXXXXXXXXXXXXXXXXX', 
     secretAccessKey : 'XXXXXXXXXXXXXXXXXXXXXXXXXXX', 
     region : 'XXXXXX' 
    } 

    var params = { 
     Bucket: bucketName + '/'+ bucketPath, Key: fileName, Expires: 60 
    }; 

    var s3 = new AWS.S3(options); 
    var url = s3.getSignedUrl('getObject', params); 
    return url; 
} 

所以当视频在新窗口中打开,他们开始得到下载在浏览器的底部。但是对于一些未知的视频,他们在一个窗口中打开并开始播放。我怎样才能在angularjs中阻止这个。什么是建议的解决方法,以及其他人如何处理这类问题?

我没有谷歌,但大多数的stackoverflow答案在这里说打开窗口和浏览器中的文件自动下载它。

回答

1

试试这个解决方案它可以帮助你。 enter link description here

查看:

<a href="#" ng-click="downloadVideo(video)">Download Video</a> 
<a id="ExportToExcel" style="display: none;"></a> 

控制器:

$scope.downloadVideo = function (video) { 
    videoLocation = video.video_location; 
    var bucketPath = videoLocation.substring(0, videoLocation.lastIndexOf("/") + 1); 
    bucketPath = bucketPath.substring(0, bucketPath.length - 1); 
    var fileName = videoLocation.substring(videoLocation.lastIndexOf("/") + 1, videoLocation.length); 
    var videoSignedUrl = VideoFactory.downloadVideo(bucketPath,fileName);    
    document.getElementById("ExportToExcel").href = videoSignedUrl;  
    document.getElementById("ExportToExcel").click(); 
} 
+0

我以前试过这个。不起作用 – VishwaKumar 2014-10-16 14:17:25

+0

我发布了适合我的工作。请看我的回答 – VishwaKumar 2014-10-16 14:23:08

+0

我也试过这个。在这种情况下工作正常。 – 2014-10-20 15:36:55

0

是:工作的视频上传到S3期间使得视频作为附件诀窍:

options = { 
    accessKeyId : 'xxxxxxxxxxxxxxxxxxxxxxx', 
    secretAccessKey : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
    region : 'xxxxxx' 
} 

var s3 = new AWS.S3(options);    

var params = { 
    Bucket : bucketName + '/' + bucketStructure, 
    Key: fileName, 
    ContentType: file.type, 
    Body: file, 
    ServerSideEncryption: 'AES256', 
    ACL : 'private', 
    ContentDisposition: 'attachment; filename=' + fileName, 
    ContentType: 'application/octet-stream' 
}; 

s3.putObject(params, function(err, data) { 
    if(err) { 
    // There Was An Error With Your S3 Config 
    console.log('AWS Error : '+err.message); 
    return false; 
    } 
    else { 

    console.log('AWS Video upload done!');    

    } 
}) 

这当使用签名的url时,使视频自动强制下载。已经为我的大部分视频MIME类型工作。

相关问题