2

In order to upload a file to Backblaze B2 directly from the browser, I need to send the X-Bz-Content-Sha1 header.

This header is the SHA1 of the added file.

This is from an example using raw JS and not DropzoneJS:

const file = uploadFileInput.files[0];
const reader = new FileReader();
reader.onload = function () {
    const hash = CryptoJS.SHA1(CryptoJS.enc.Latin1.parse(reader.result));
    /* snip */
    xhr.setRequestHeader("X-Bz-Content-Sha1", hash);
};
reader.readAsBinaryString(file);

How can I generate a SHA1 of the file I add into DropzoneJS?

Here's my DropzoneJS code:

let myDropzone = new Dropzone("#dropzone", {
    acceptedFiles: "image/jpeg,image/jpg",
    maxFilesize: 100,
    init: function () {
        this.on("addedfile", function (file) {
            fetch("/api/presign-upload-url", {
                headers: {
                    "Content-Type": "application/json",
                    "Accept": "application/json",
                    "X-Requested-With": "XMLHttpRequest",
                    "X-CSRF-Token": csrfToken
                },
                method: "post",
                credentials: "same-origin",
                body: JSON.stringify({
                    key: file.name
                })
            })
            .then(function (response) {
                return response.json();
             )
            .then(function (json) {
                console.log(file);
                // I don't know how to generate the SHA1 here, `file`
                // seems like the wrong object to work with?
                let hash = "2";
                myDropzone.options.headers = {
                    "Content-Type": file.type,
                    "Authorization": json.upload_url.authorizationToken,
                    "X-Bz-File-Name": file.name,
                    "X-Bz-Content-Sha1": hash,
                };
                myDropzone.options.url = json.upload_url.uploadUrl;
                myDropzone.processFile(file);
            });
        });
    }
});
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257

0 Answers0