javascript - 输入捕获文件减少和转换问题
问题描述
我尝试从相机(Samsung Tab s7)捕获 Html5 输入,然后调整大小(我的主要目标是减小图像大小),然后通过我的控制器上传 post 方法。但是当我上传时,我的文件识别出 .bin 并像这样保存。我无法保存 jpeg。我该如何解决?
HTML:
<input type="file" id="captureImageInput" style="display:none;" accept="image/*" capture>
JS:
const fileInput = document.getElementById('captureImageInput');
fileInput.addEventListener('change', e => {
if (e.target.files.length == 0) {
// No file captured, ignore
return;
}
else{
var imageFile = e.target.files[0];
//--resize image START
var canvas = document.createElement("canvas");
var img1 = document.createElement("img");
img1.setAttribute('src', imageFile);
var ctx = canvas.getContext("2d");
ctx.drawImage(img1, 0, 0);
var MAX_WIDTH = 800;
var MAX_HEIGHT = 600;
var width = img1.width;
var height = img1.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
}
else if (width == 0 && height == 0)
{
width = MAX_WIDTH;
height = MAX_HEIGHT;
}
else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img1, 0, 0, width, height);
var dataurl = canvas.toDataURL("image/jpeg");
var blobBin = atob(dataurl.split(',')[1]);
var array = [];
for (var i = 0; i < blobBin.length; i++) {
array.push(blobBin.charCodeAt(i));
}
var resizedFileBlob = new Blob([new Uint8Array(array)], { type: 'image/jpeg' });
//--resize image END
var fullFileName = serialNo + "-" + stockNo;
var fileImage = new File([resizedFileBlob], fullFileName);
var myFormData = new FormData();
myFormData.append('FormFile', fileImage);
$.ajax({
//POST OPERATION TO CONTROLLER
});
}
});
控制器端:
[HttpPost]
public JsonResult SavePhysicalPath(FileModel myFormData)
{
//update physical path operatıons..
}
文件模型类:
public class FileModel
{
public string FileFolderPath { get; set; }
public string Files { get; set; }
public IFormFile[] FormFile { get; set; }
}
解决方案
根据您的代码,我认为问题与您使用此行创建了一个新文件有关 var fileImage = new File([resizedFileBlob], fullFileName);
。
我建议您可以删除此行并直接使用resizedFileBlob
作为参数并传递给后端。
我已经创建了一个如下的测试演示,您可以参考它。
JS:
<script type="text/javascript">
$(document).ready(function () {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var img = new Image();
img.onload = function () {
// set size proportional to image
canvas.height = canvas.width * (img.height / img.width);
// step 1 - resize to 50%
var oc = document.createElement('canvas'),
octx = oc.getContext('2d');
oc.width = img.width * 0.5;
oc.height = img.height * 0.5;
octx.drawImage(img, 0, 0, oc.width, oc.height);
// step 2
octx.drawImage(oc, 0, 0, oc.width * 0.5, oc.height * 0.5);
// step 3, resize to final size
ctx.drawImage(oc, 0, 0, oc.width * 0.5, oc.height * 0.5,
0, 0, canvas.width, canvas.height);
}
img.src = "https://i.imgur.com/SHo6Fub.jpg";
var cav = document.getElementById("canvas");
const file = dataURLtoBlob(cav.toDataURL());
var fileImage = new File([file], "test");
const fd = new FormData;
fd.append('image', fileImage);
$.ajax({
type: 'POST',
url: '/api/Foo',
data: fd,
processData: false,
contentType: false
});
});
function dataURLtoBlob(dataURL) {
let array, binary, i, len;
binary = atob(dataURL.split(',')[1]);
array = [];
i = 0;
len = binary.length;
while (i < len) {
array.push(binary.charCodeAt(i));
i++;
}
return new Blob([new Uint8Array(array)], {
type: 'image/png'
});
};
</script>
看法:
<img src="https://i.imgur.com/SHo6Fub.jpg" width="300" height="234">
<canvas id="canvas" width=300></canvas>
结果:
推荐阅读
- javascript - javascript for loop inside for 循环计算
- c++ - 使用长度未知的向量的向量
- computer-vision - 哪种 CNN 架构适合植物叶片病害检测?
- php - 将文件转换为字符串,将这些字符串导入第 3 方下载器并下载文件
- javascript - 如何判断是否在我的页面上显示较少的 GPU 密集型图形?
- php - php生成一个下拉年份为-5年和+5年的选择框
- c# - C#,控制台应用程序的重定向进程的 WaitForInputIdle() 是否有同样的事情?
- java - 如何在 Single 方法中为不同的端点创建对象,而不是 java 中的 if 循环
- dart - 重定向的构造函数具有不兼容的参数
- c - 为什么读取用户输入时c中的scanf会跳过字符值?