zoukankan      html  css  js  c++  java
  • 使用SAS和JavaScript前端上传Azure Bolb大文件

    问题描述:

    Azure Storage Rest API提供了对于大文件分块上传方法,分别使用Put BlockPut Block List实现相关功能

    参考链接:

    Code Sample

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>File Uploader</title>
        <script src="http://code.jquery.com/jquery-1.4.1.js"></script>
        <link rel="stylesheet" href="css/modern.css" />
        <script>
            var maxBlockSize = 256 * 1024;//Each file will be split in 256 KB.
            var numberOfBlocks = 1;
            var selectedFile = null;
            var currentFilePointer = 0;
            var totalBytesRemaining = 0;
            var blockIds = new Array();
            var blockIdPrefix = "block-";
            var submitUri = null;
            var bytesUploaded = 0;
    
            $(document).ready(function () {
                $("#output").hide();
                $("#file").bind('change', handleFileSelect);
                if (window.File && window.FileReader && window.FileList && window.Blob) {
                    // Great success! All the File APIs are supported.
                } else {
                    alert('The File APIs are not fully supported in this browser.');
                }
            });
    
            //Read the file and find out how many blocks we would need to split it.
            function handleFileSelect(e) {
                maxBlockSize = 256 * 1024;
                currentFilePointer = 0;
                totalBytesRemaining = 0;
                var files = e.target.files;
                selectedFile = files[0];
                $("#output").show();
                $("#fileName").text(selectedFile.name);
                $("#fileSize").text(selectedFile.size);
                $("#fileType").text(selectedFile.type);
                var fileSize = selectedFile.size;
                if (fileSize < maxBlockSize) {
                    maxBlockSize = fileSize;
                    console.log("max block size = " + maxBlockSize);
                }
                totalBytesRemaining = fileSize;
                if (fileSize % maxBlockSize == 0) {
                    numberOfBlocks = fileSize / maxBlockSize;
                } else {
                    numberOfBlocks = parseInt(fileSize / maxBlockSize, 10) + 1;
                }
                console.log("total blocks = " + numberOfBlocks);
                var baseUrl = $("#sasUrl").val();
                var indexOfQueryStart = baseUrl.indexOf("?");
                submitUri = baseUrl.substring(0, indexOfQueryStart) + '/' + selectedFile.name + baseUrl.substring(indexOfQueryStart);
                console.log(submitUri);
            }
    
            var reader = new FileReader();
    
            reader.onloadend = function (evt) {
                if (evt.target.readyState == FileReader.DONE) { // DONE == 2
                    var uri = submitUri + '&comp=block&blockid=' + blockIds[blockIds.length - 1];
                    var requestData = new Uint8Array(evt.target.result);
                    $.ajax({
                        url: uri,
                        type: "PUT",
                        data: requestData,
                        processData: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader('x-ms-blob-type', 'BlockBlob');
                            xhr.setRequestHeader('Content-Length', requestData.length);
                        },
                        success: function (data, status) {
                            console.log(data);
                            console.log(status);
                            bytesUploaded += requestData.length;
                            var percentComplete = ((parseFloat(bytesUploaded) / parseFloat(selectedFile.size)) * 100).toFixed(2);
                            $("#fileUploadProgress").text(percentComplete + " %");
                            uploadFileInBlocks();
                        },
                        error: function (xhr, desc, err) {
                            console.log(desc);
                            console.log(err);
                        }
                    });
                }
            };
    
            function uploadFileInBlocks() {
                if (totalBytesRemaining > 0) {
                    console.log("current file pointer = " + currentFilePointer + " bytes read = " + maxBlockSize);
                    var fileContent = selectedFile.slice(currentFilePointer, currentFilePointer + maxBlockSize);
                    var blockId = blockIdPrefix + pad(blockIds.length, 6);
                    console.log("block id = " + blockId);
                    blockIds.push(btoa(blockId));
                    reader.readAsArrayBuffer(fileContent);
                    currentFilePointer += maxBlockSize;
                    totalBytesRemaining -= maxBlockSize;
                    if (totalBytesRemaining < maxBlockSize) {
                        maxBlockSize = totalBytesRemaining;
                    }
                } else {
                    commitBlockList();
                }
            }
    
            function commitBlockList() {
                var uri = submitUri + '&comp=blocklist';
                console.log(uri);
                var requestBody = '<?xml version="1.0" encoding="utf-8"?><BlockList>';
                for (var i = 0; i < blockIds.length; i++) {
                    requestBody += '<Latest>' + blockIds[i] + '</Latest>';
                }
                requestBody += '</BlockList>';
                console.log(requestBody);
                $.ajax({
                    url: uri,
                    type: "PUT",
                    data: requestBody,
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('x-ms-blob-content-type', selectedFile.type);
                        xhr.setRequestHeader('Content-Length', requestBody.length);
                        //xhr.setRequestHeader('x-ms-blob-content-md5', "OcEHvHuHGvmhdZ5vBtxx0Q=="); //设置组合后的blob的md5值,并不会做校验
                    },
                    success: function (data, status) {
                        console.log(data);
                        console.log(status);
                    },
                    error: function (xhr, desc, err) {
                        console.log(desc);
                        console.log(err);
                    }
                });
    
            }
            function pad(number, length) {
                var str = '' + number;
                while (str.length < length) {
                    str = '0' + str;
                }
                return str;
            }
        </script>
    </head>
    <body>
        <form>
            <div style="margin-left: 20px;">
                <h1>File Uploader</h1>
                <p>
                    <strong>SAS URI</strong>:
                    <br />
                    <span class="input-control text">
                        <input type="text" id="sasUrl" style=" 50%"
                               value="" />
                    </span>
                </p>
                <p>
                    <strong>File To Upload</strong>:
                    <br />
                    <span class="input-control text">
                        <input type="file" id="file" name="file" style=" 50%" />
                    </span>
                </p>
                <div id="output">
    
                    <strong>File Properties:</strong>
                    <br />
                    <p>
                        Name: <span id="fileName"></span>
                    </p>
                    <p>
                        File Size: <span id="fileSize"></span> bytes.
                    </p>
                    <p>
                        File Type: <span id="fileType"></span>
                    </p>
                    <p>
                        <input type="button" value="Upload File" onclick="uploadFileInBlocks()" />
                    </p>
                    <p>
                        <strong>Progress</strong>: <span id="fileUploadProgress">0.00 %</span>
                    </p>
                </div>
            </div>
            <div>
            </div>
        </form>
    </body>
    </html>
    

    注意事项

    • MD5值计算方法
    • 完整项目GitHub网址
    • 该示例并未进行MD5值的校验操作,如果需要校验需要在请求头中添加Content-MD5参数,Put Block List并不提供对上传完成后完整blob的校验,只能通过x-ms-blob-content-md5设置blob的值,否则后台默认并不会自动生成MD5值
  • 相关阅读:
    设计模式13---设计模式之观察者模式(Observer)(行为型)
    codeforces 166C Median
    HDU 1176 免费馅饼
    老鸟的Python新手教程
    setsockopt()使用方法(參数具体说明)
    RelativeLayout经常使用属性介绍
    java多线程模拟生产者消费者问题,公司面试常常问的题。。。
    JAVA实现HTTPserver端
    进程间通信_03命名管道
    expdp&amp;impdp
  • 原文地址:https://www.cnblogs.com/taro/p/6607299.html
Copyright © 2011-2022 走看看