0

我目前正在使用此代码从本地磁盘选择一个文件到我的 api:

<script language="javascript" type="text/javascript">
$(document).ready(function(){

$(':file').on('change', function () {
  var file = this.files[0];

    if (file.type !== "video/mp4" && file.type!== "video/quicktime") {
        alert("Content must be video .mp4 or .mov")
    }

$(':button').on('click', function () {
    if (file.type == "video/mp4" || file.type == "video/quicktime"){
  $.ajax({
    // Your server script to process the upload
    url: 'azureAPI',
    type: 'POST',

    // Form data
    data: new FormData($('form')[0]),

    // Tell jQuery not to process data or worry about content-type
    // You *must* include these options!
    cache: false,
    contentType: false,
    processData: false,

    // Custom XMLHttpRequest
    xhr: function () {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        // For handling the progress of the upload
        myXhr.upload.addEventListener('progress', function (e) {
          if (e.lengthComputable) {
            $('progress').attr({
              value: e.loaded,
              max: e.total,
            });
          }
        }, false);
      }
      return myXhr;
    }
  });
} else {
    alert ("File type must be .mp4 or .mov")
}
});
});
});
</script>

这会以以下形式发送(我假设是)二进制数据:

���
1!QAa"q2B���R�#3br��u�����S6C$%��5�cts�T&D4��U��d���e!1AQa2"q�#����3��B���X"��?��!=��W�u�ٗ�-2���?����ۯ�Կ�i���t����M���Y�-��-Vdϊ�P�&lt;�&lt;U#TY]K��dW
���

我正在使用 Azure,现在尝试将其发送到 Microsoft Video Indexer,它表示将数据作为正文中的 multipart/form-data 发送。(见https://api-portal.videoindexer.ai/docs/services/Operations/operations/Upload-Video?)

我尝试在正文中发送二进制数据,但它说它需要字符串/缓冲区。

然后我尝试将正文中的二进制数据发送为var body = Buffer.from(req.body,'binary')

哪个发送了,但 VI 回复说索引数据存在问题,可能是因为我发送的编码错误?

为了解决这个问题,我现在尝试先将该二进制数据保存到块 blob,然后我将调用该 url,但是我无法使用以下方法将二进制数据保存到 Azure 块 blob:

var buf = Buffer.from(req.body, 'binary');
blobService.createBlockBlobFromText(container, 'fileName.mp4', buf, {contentSettings: {contentType: 'video/mp4', contentEncoding: 'binary'}}, function (error, result, response) {
    if(!error){
        callback('uploaded');
    } else {
        callback('nope');
    }

});

我尝试了这个,一开始没有contentSettings,但将数据保存为contentType: application/octet-stream未作为视频打开的数据。然后我添加了contentType,并继续尝试添加contentEncoding

这保存了正确的contentType但仍然无法打开视频。

有谁知道如何正确编码数据以首先直接发送到视频索引器,或者其次将二进制数据保存到 Azure blob 存储?

感谢您的任何指点,如果我遗漏了任何内容,我们深表歉意。

4

1 回答 1

2

根据我的测试,如果要使用 Azure 函数将文件上传到 Azure blob,请参考以下代码。

前端

<!DOCTYPE html>
<html>
  <script type="text/javascript"
 src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.js">
</script>
<body>

 <form enctype="multipart/form-data">
    <input name="file" type="file" accept="video/*"/>
    <input type="button" value="Upload" />
</form>
<progress></progress>

<script language="javascript" type="text/javascript">
$(document).ready(function(){

$(':file').on('change', function () {
  var file = this.files[0];
    if (file.type !== "video/mp4" && file.type!== "video/quicktime") {
        alert("Content must be video .mp4 or .mov")
    }

$(':button').on('click', function () {

  if (file.type == "video/mp4" || file.type == "video/quicktime"){

    $.ajax({
    // Your server script to process the upload
    url: '',
    type: 'POST',
    crossDomain: true,
    enctype: 'multipart/form-data',
    // Form data
    data: new FormData($('form')[0]),

    // Tell jQuery not to process data or worry about content-type
    // You *must* include these options!
    cache: false,
    contentType: false,
    processData: false,

    success :  function(data){console.log(data);},

    // Custom XMLHttpRequest
    xhr: function () {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        // For handling the progress of the upload

        myXhr.upload.addEventListener('progress', function (e) {
          if (e.lengthComputable) {
            $('progress').attr({
              value: e.loaded,
              max: e.total,
            });
          }
        }, false);
      }
      return myXhr;
    }
  });
} else {
    alert ("File type must be .mp4 or .mov")
}
});





});



});

</script>

</body>
</html>

天青功能

var multipart = require('parse-multipart')
var azure = require('azure-storage');
var getStream = require('into-stream')
module.exports = async function (context, request) {
    context.log('JavaScript HTTP trigger function processed a request.');
    // encode body to base64 string
    var bodyBuffer = Buffer.from(request.body);

    var boundary = multipart.getBoundary(request.headers['content-type']);
    // parse the body
    var parts = multipart.Parse(bodyBuffer, boundary);

    const accountname ="blobstorage0516";
            const key = "key";
            const containerName="test";
            var retryOperations = new azure.ExponentialRetryPolicyFilter();
            const blobClient  =azure.createBlobService(accountname,key).withFilter(retryOperations);
            blobClient.createContainerIfNotExists(containerName, function (error) {
                if (error) {
                  context.log(error);
                }
            });

            var options = {
                contentSettings:{contentType: parts[0].type},
                metadata: { fileName: parts[0].filename },
                blockSize:8*1024*1024,
                parallelOperationThreadCount:20,
                timeoutIntervalInMs:30*60*1000
              };
              var stream =getStream(parts[0].data)
              context.log("start")

              var result="ok"
            var speedsummary= blobClient.createBlockBlobFromStream(containerName,parts[0].filename,stream,parts[0].data.length,options,function (error) {


              if (error != null) {
               result=error
              } else {



              }})

               context.res = { body : { results : result}};
              context.done(); 


};

在此处输入图像描述 在此处输入图像描述

您还可以从bloburl访问视频

于 2020-02-12T02:38:43.240 回答