我已编写代码,使用aws javascript sdk下载上传到amazon s3的视频。一切正常,但有些视频在浏览器中打开并开始播放。以下是代码:
查看:
<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答案都说在窗口中打开文件,浏览器自动下载它。
答案 0 :(得分: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();
}
答案 1 :(得分: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!');
}
})
这会使视频在使用签名网址时自动下载。已经为我的大多数视频mime类型工作。