使用javascript将base64编码图像发布到服务器 [英] post base64 encode image to server using javascript

查看:92
本文介绍了使用javascript将base64编码图像发布到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将图像上传到服务器,图像来自,然后我想通过python flask框架通过ajax,服务器端将图像发布到服务器,它需要base64编码格式,问题是我如何使用javascript将图像转换为base64格式

upload image to server ,image is from ,then i want to post image to server through ajax, server side using python flask framework, it needs base64 encoding format ,the question is how can i use javascript to convert image to base64 format.

	$('.img-upload-btn').click(function(event) {
		$("#img-upload").click();
	});

	$('#img-upload').on('change', function(event) {
		event.preventDefault();
		var img = $("#img-upload")[0].files[0];
		console.log(toDataUrl(img.name));

		var img_data = {
			"spec_id": 212,
			"file": img
		};
		console.log(img);
		$.ajax({
			url: 'http://10.0.0.75:5000/api/check_specification',
			type: 'POST',
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success: function(data) {
				alert(data);
			},

			failure:function(errorMsg) {
				alert(errorMsg);
			}
		});
		
	});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn-search full-width img-upload-btn">upload-img</button>
<input type="file" id="img-upload" >

推荐答案

可以在base64中获得图像.

Something like this you can get image in base64.

fileChange(e) {
    /* any way to get the object input */
    var file    = document.querySelector('input[type=file]');
    let reader = new FileReader();
    if(file.files[0]) {
        reader.onload = () => {
        imgBase64 = reader.result;
        console.log(reader.result);
        //your request
      }
      reader.readAsDataURL(file[0]);
    }
}

这篇关于使用javascript将base64编码图像发布到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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