将blob转换为图像文件

时间:2018-05-25 22:48:57

标签: javascript file-upload blob

我正在尝试转换从input 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);
        }

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

2 个答案:

答案 0 :(得分:2)

这是你可以从你的" myimage"中获取一个调整大小的jpeg文件的方法。使用Canvas的元素。

我评论了每一行代码,以便你能理解我在做什么。

// 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.

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

// data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ...9oADAMBAAIRAxEAPwD/AD/6AP/Z"

您可以将该变量设置为图像源,它将显示您的图像,或者您可以将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});
}

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

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

现在发送&#34; jpegBlob&#34;用ajax

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

oReq.send(jpegBlob);

答案 1 :(得分:1)

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

要执行此操作,请将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();