2014-10-08 41 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類型工作。

相關問題