提问人:mark davies 提问时间:6/11/2019 最后编辑:mark davies 更新时间:6/12/2019 访问量:182
使用 VB.Net 将视频 url 传递给 javascript,并在其中创建缩略图
Using VB.Net to pass a video url to javascript where it creates a thumbnail
问:
我正在尝试使用 VB.Net 从上传的视频中生成并保存缩略图。在获得以下 Javascript 之前,我尝试了很多资源。如果您按原样复制并粘贴它,它可以正常工作,但您必须选择一个本地文件,它会生成一个缩略图。我需要它从给定的 url 创建缩略图,我从 VB.Net 代码中传递它,然后将其保存在同一文件夹中。但是,我真的不了解 Javascript..只是 VB.Net。有谁知道如何做到这一点?非常感谢。
这是我在VB中的代码,一旦用户上传视频,它就会保存文件。
Protected Sub AjaxFileUpload1_UploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs) Handles AjaxFileUpload1.UploadComplete
'Save the original image
Dim filename As String = RemoveSpecialChars(e.FileName)
Dim imageFilename As String = DateTime.Now.Ticks.ToString() + "_" + filename
Dim acc As New accounts(Membership.GetUser.ProviderUserKey)
AjaxFileUpload1.SaveAs("E:\kunden\homepages\19\d664110395\www\MyUrl\catalog\videos\" & imageFilename)
End Sub
我尝试将 imagefilename 传递给 javascript,但它正在寻找输入值。
<script type='text/javascript'>
document.getElementsByTagName('input')[0].addEventListener('change', function(event) {
var file = event.target.files[0];
var fileReader = new FileReader();
if (file.type.match('image')) {
fileReader.onload = function() {
var img = document.createElement('img');
img.src = fileReader.result;
document.getElementsByTagName('div')[0].appendChild(img);
};
fileReader.readAsDataURL(file);
} else {
fileReader.onload = function() {
var blob = new Blob([fileReader.result], {type: file.type});
var url = URL.createObjectURL(blob);
var video = document.createElement('video');
var timeupdate = function() {
if (snapImage()) {
video.removeEventListener('timeupdate', timeupdate);
video.pause();
}
};
video.addEventListener('loadeddata', function() {
if (snapImage()) {
video.removeEventListener('timeupdate', timeupdate);
}
});
var snapImage = function() {
var canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
var image = canvas.toDataURL();
var success = image.length > 100000;
if (success) {
var img = document.createElement('img');
img.src = image;
document.getElementsByTagName('div')[0].appendChild(img);
URL.revokeObjectURL(url);
}
return success;
};
video.addEventListener('timeupdate', timeupdate);
video.preload = 'metadata';
video.src = url;
// Load video in Safari / IE11
video.muted = true;
video.playsInline = true;
video.play();
};
fileReader.readAsArrayBuffer(file);
}
});
</script>
<style>
div {
line-height: 200px;
}
img {
max-width: 200px;
max-height: 200px;
padding: 5px;
vertical-align: middle;
text-align: center;
}
@supports (object-fit: cover) {
img {
width: 200px;
height: 200px;
object-fit: cover;
}
}
</style>
答: 暂无答案
评论