如何用canvas.toDataURL的结果替换文件输入的内容? [英] How to replace a file input's content by the result of canvas.toDataURL?

查看:93
本文介绍了如何用canvas.toDataURL的结果替换文件输入的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码(受 HTML5预先调整大小的图片在上传之前的主要回答启发)从<input type="file">中获取所选的图像文件,然后将其重新压缩为质量为50%的JPG,并将其压缩为变量dataurl.

The following code (inspired by the main answer of HTML5 Pre-resize images before uploading) takes the selected image file from the <input type="file">, recompresses it to a JPG with quality 50% into the variable dataurl.

问题:如何用此新压缩的文件替换<input type="file">的内容?这样,在提交<form>时,将提交的是这个新文件吗?

Question: How to replace the content of the <input type="file"> by this newly-compressed file? So that when submitting the <form>, it's this new file which will be submitted?

注意:我不想使用AJAX进行上传,而是使用<form>的标准POST. 已发布"文件应为dataurl,即用户在<input type="file">中选择的文件应与dataurl生成的文件相同.

Note: I don't want to use AJAX for the upload, but the standard POST of the <form>. The "posted" file should be dataurl, i.e. it should be as if the file selected by the user in the <input type="file"> were the file generated by dataurl.

function doit() {
    var input = document.getElementById('file'),
        canvas = document.getElementById('canvas');
    var file = input.files[0];
    var img = document.createElement("img");
    var reader = new FileReader();  
    reader.onload = function(e) { img.src = e.target.result }
    reader.readAsDataURL(file);
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    var dataurl = canvas.toDataURL("image/jpeg", 0.5);
    console.log(dataurl);
}    

<form action="/upload" method="post">
<input type="file" onchange="doit();" id="file" /><br><br>
<input type="submit" />
</form>
<canvas id="canvas" style="display: none" />  

推荐答案

似乎我们无法修改<input type="file">,但可以将数据添加到另一个文本字段(如@PatrickEvans所建议)或<input type="hidden">:

It seems we cannot modify the <input type="file">, but we can add the data to another text field (as advised by @PatrickEvans) or an <input type="hidden">:

function doit() {
    var file = document.getElementById('file').files[0],
        canvas = document.getElementById('canvas'),
        hidden = document.getElementById('hidden'),
        ctx = canvas.getContext("2d"),
        img = document.createElement("img"),
        reader = new FileReader();  
    
    reader.onload = function(e) { 
        img.src = e.target.result;
    }
    
    img.onload = function () { 
        ctx.drawImage(img, 0, 0);
        hidden.value = canvas.toDataURL("image/jpeg", 0.5);
    }
    reader.readAsDataURL(file);
}

<input type="file" onchange="doit();" id="file" />

<form action="/upload" method="post">
<input type="hidden" id="hidden" />
<input type="submit" />
</form>

<canvas id="canvas" style="display: none" />

<form>中的输出hidden字段是base64编码的,例如:

The output hidden field in the <form> is base64-encoded, i.e. something like:

data:image/jpeg;base64,/9j/4AAQSkZJRgAB...

这篇关于如何用canvas.toDataURL的结果替换文件输入的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆