简体   繁体   English

将视频从客户端发送到服务器

[英]Sending videos from the client to the server

I recently read about the File API which allows the client to use files from the file system in the website. 我最近阅读了有关File API的信息,该文件API允许客户端使用网站中文件系统中的文件。

I did all the stuff of adding the file input tag and reading the file and everything. 我做了所有添加文件输入标签,读取文件和所有内容的工作。

I read the file as ArrayBuffer using a FileReader and used a Uint8Array view on it, then I send the view to the server. 我使用FileReader将文件读取为ArrayBuffer并在其上使用了Uint8Array视图,然后将视图发送到服务器。

I tried it with txt, pdf and images and it worked fine. 我尝试使用txt,pdf和图像,但效果很好。 But when to use it with video files the computer lagged that and I didn't even wait and closed the browser! 但是,当将它与视频文件一起使用时,计算机却滞后了,我什至没有等待并关闭浏览器!

Here's the code 是代码

Why did the lag happen with video files and is there a way to avoid it?! 为什么视频文件会出现延迟,有办法避免这种情况吗?

I gone through the code, you are appending the read data pushed into the array which may be the reason why it lags. 我遍历了代码,您将读取的数据追加到数组中,这可能是它滞后的原因。

You need to upload the video in chunks. 您需要分块上传视频。 To make a chunk of file you choose, you can take a look here HTML5 file reader api 要制作您选择的文件块,您可以在此处查看HTML5文件阅读器API

  function readBlob(opt_startByte, opt_stopByte) {
    var files = document.getElementById('files').files;
    if (!files.length) {
      alert('Please select a file!');
      return;
    }

    var file = files[0];
    var start = parseInt(opt_startByte) || 0;
    var stop = parseInt(opt_stopByte) || file.size - 1;

    var reader = new FileReader();

    // If we use onloadend, we need to check the readyState.
    reader.onloadend = function(evt) {
      if (evt.target.readyState == FileReader.DONE) { // DONE == 2
        document.getElementById('byte_content').textContent = evt.target.result;
        document.getElementById('byte_range').textContent = 
            ['Read bytes: ', start + 1, ' - ', stop + 1,
             ' of ', file.size, ' byte file'].join('');
      }
    };

    var blob = file.slice(start, stop + 1);
    reader.readAsBinaryString(blob);
    }

    document.querySelector('.readBytesButtons')
      .addEventListener('click', function(evt) {
    if (evt.target.tagName.toLowerCase() == 'button') {
      var startByte = evt.target.getAttribute('data-startbyte');
      var endByte = evt.target.getAttribute('data-endbyte');
      readBlob(startByte, endByte);
    }
  }, false);

You need to modify according to your requirements. 您需要根据需要进行修改。 Here when you get the chunk, make a request to upload it or play it. 在这里,当您获得块时,请求上传或播放它。

Also you can play with you video file here Upload your Video file here to test 您也可以在这里播放视频文件在这里上传视频文件进行测试

Hope this solves your problem. 希望这能解决您的问题。 To get this working you need to handle the chunks properly on the backend as well. 为了使此工作正常进行,您还需要在后端正确处理这些块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM