将Blob转换为图像文件

Dav*_*ave 1 javascript blob file-upload

我试图转换从输入type = file元素捕获的图像文件而没有成功。以下是我的JavaScript代码

        var img = document.getElementById('myimage');
        var file = document.querySelector('input[type=file]').files[0];
        var reader = new FileReader();
        reader.addEventListener("load", function () {
            var theBlob = reader.result;
            theBlob.lastModifiedDate = new Date();
            theBlob.name = file;
            img.src = theBlob;
            var newfile = new File([theBlob], "c:files/myfile.jpg");

        }, false);

        if (file) {
            reader.readAsDataURL(file);
        }
Run Code Online (Sandbox Code Playgroud)

该图像会正确显示在img元素中,但File命令不会创建myfile.jpg图像文件。我试图捕获图像,然后使用javascript调整大小,然后再将其上传到服务器。有人知道为什么不创建图像文件吗?更好的是,没有人有代码说明如何在客户端上调整图像文件的大小,然后将其上传到服务器吗?

use*_*641 8

您无法直接从浏览器操作硬盘驱动器。您可以做的是创建一个可以下载的链接。

为此,请更改var newfile = new File([theBlob], "c:files/myfile.jpg");为:

const a = document.createElement('a');
a.setAttribute('download', "some image name");
a.setAttribute('href', theBlob);

a.click();
Run Code Online (Sandbox Code Playgroud)


YAH*_*ves 5

这是使用Canvas从“ myimage”元素中获取大小调整后的jpeg文件的方法。

我注释了每一行代码,以便您了解我在做什么。

// Set the Width and Height you want your resized image to be
var width = 1920; 
var height = 1080; 


var canvas = document.createElement('canvas');  // Dynamically Create a Canvas Element
canvas.id = "extractFileCanvas";  // Give the canvas an id
canvas.width  = width;  // Set the width of the Canvas
canvas.height = height;  // Set the height of the Canvas
canvas.style.display   = "none";  // Make sure your Canvas is hidden
document.body.appendChild(canvas);  // Insert the canvas into your page


var c=document.getElementById("extractFileCanvas");  // Get canvas from page
var ctx=c.getContext("2d");  // Get the "CTX" of the canvas 
var img=document.getElementById("myimage");  // The id of your image container
ctx.drawImage(img,0,0,width,height);  // Draw your image to the canvas


var jpegFile = c.toDataURL("image/jpeg"); // This will save your image as a 
                                               //jpeg file in the base64 format.
Run Code Online (Sandbox Code Playgroud)

javascript变量“ jpegFile”现在包含以URL:// Base64格式编码的图像。看起来像这样:

// data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ...9oADAMBAAIRAxEAPwD/AD/6AP/Z"
Run Code Online (Sandbox Code Playgroud)

您可以将该变量用作图像源,它将显示您的图像,也可以将Base64编码的字符串上载到服务器。

编辑:如何将文件转换为Blob并将其上传到服务器

// This function is used to convert base64 encoding to mime type (blob)
function base64ToBlob(base64, mime) 
{
    mime = mime || '';
    var sliceSize = 1024;
    var byteChars = window.atob(base64);
    var byteArrays = [];

    for (var offset = 0, len = byteChars.length; offset < len; offset += sliceSize) {
        var slice = byteChars.slice(offset, offset + sliceSize);

        var byteNumbers = new Array(slice.length);
        for (var i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }

        var byteArray = new Uint8Array(byteNumbers);

        byteArrays.push(byteArray);
    }

    return new Blob(byteArrays, {type: mime});
}
Run Code Online (Sandbox Code Playgroud)

现在清理base64,然后将其传递到上面的函数中:

var jpegFile64 = jpegFile.replace(/^data:image\/(png|jpeg);base64,/, "");
var jpegBlob = base64ToBlob(jpegFile64, 'image/jpeg');  
Run Code Online (Sandbox Code Playgroud)

现在发送带有Ajax的“ jpegBlob”

var oReq = new XMLHttpRequest();
oReq.open("POST", url, true);
oReq.onload = function (oEvent) {
  // Uploaded.
};

oReq.send(jpegBlob);
Run Code Online (Sandbox Code Playgroud)