[英]How to post an image in base64 encoding via .ajax?
I have some javascript code that uploads an image to a server. 我有一些javascript代码将图像上传到服务器。 Below is the ajax call that works correctly.
以下是正常工作的ajax调用。
$.ajax({
url: 'https://api.projectoxford.ai/vision/v1/analyses?',
type: 'POST',
contentType: 'application/json',
data: '{ "Url": "http://images.takungpao.com/2012/1115/20121115073901672.jpg" }',
})
I now need to upload the image aa base64 encoding eg 我现在需要上传图像aa base64编码,例如
data: 'data:image/jpeg;base64,/9j/4AAQSkZJRgA..........gAooooAKKKKACiiigD//Z'
But that doesn't work, ie the server doesn't recognise the data I send and complains. 但这不起作用,即服务器无法识别我发送和抱怨的数据。
Does anyone know what the correct format is for sending base64 encoded data in the AJAX call ? 有谁知道在AJAX调用中发送base64编码数据的正确格式是什么?
Thanks for all the answers which helped me along. 感谢所有帮助我的答案。
I had also posted the question to the forums on https://social.msdn.microsoft.com/Forums/en-US/807ee18d-45e5-410b-a339-c8dcb3bfa25b/testing-project-oxford-ocr-how-to-use-a-local-file-in-base64-for-example?forum=mlapi (more Project Oxford specific) and between their answers and your's I've got a solution. 我还在论坛上发布了这个问题:https://social.msdn.microsoft.com/Forums/en-US/807ee18d-45e5-410b-a339-c8dcb3bfa25b/testing-project-oxford-ocr-how-to- use-a-local-file-in-base64-for-example?forum = mlapi (更多Project Oxford特定),在他们的答案和你的答案之间,我有一个解决方案。
processData:false
and contentType: 'application/octet-stream'
options in the .ajax call processData:false
和contentType: 'application/octet-stream'
选项 So my solution looks like this 所以我的解决方案看起来像这样
First a function to make the blob (This is copied verbatim from someone more gifted than I) 首先是一个制作blob的函数(这是从比我更有天赋的人逐字复制的)
makeblob = function (dataURL) {
var BASE64_MARKER = ';base64,';
if (dataURL.indexOf(BASE64_MARKER) == -1) {
var parts = dataURL.split(',');
var contentType = parts[0].split(':')[1];
var raw = decodeURIComponent(parts[1]);
return new Blob([raw], { type: contentType });
}
var parts = dataURL.split(BASE64_MARKER);
var contentType = parts[0].split(':')[1];
var raw = window.atob(parts[1]);
var rawLength = raw.length;
var uInt8Array = new Uint8Array(rawLength);
for (var i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
return new Blob([uInt8Array], { type: contentType });
}
and then 然后
$.ajax({
url: 'https://api.projectoxford.ai/vision/v1/ocr?' + $.param(params),
type: 'POST',
processData: false,
contentType: 'application/octet-stream',
data: makeblob('data:image/jpeg;base64,9j/4AAQSkZJRgA..........gAooooAKKKKACiiigD//Z'
})
.done(function(data) {alert("success");})
.fail(function() {alert("error");});
This is some working code from my own application. 这是我自己的应用程序中的一些工作代码。 You will need to change the
contentType
and data
args in your ajax operations. 您需要更改ajax操作中的
contentType
和data
args。
var video = that.vars.video;
var code = document.createElement("canvas");
code.getContext('2d').drawImage(video, 0, 0, code.width, code.height);
var img = document.createElement("img");
img.src = code.toDataURL();
$.ajax({
url: '/scan/submit',
type: 'POST',
data: { data: code.toDataURL(), userid: userid },
success: function (data) {
if (data.error) {
alert(data.error);
return;
}
// do something here.
},
error : function (r, s, e) {
alert("Unexpected error:" + e);
console.log(r);
console.log(s);
console.log(e);
}
});
//After received the foto, convert to byte - C# code
Dim imagem = imagemBase64.Split(",")(1)
Dim bytes = Convert.FromBase64String(imagem)
You load the image in canvas, not necessary upload to server. 您在画布中加载图像,而不必上传到服务器。
var ctx = canvas.getContext('2d');
ctx.drawImage(img, selection.x, selection.y, selection.w, selection.h, 0, 0, canvas.width, canvas.height);
var ctxPreview = avatarCanvas.getContext('2d');
ctxPreview.clearRect(0, 0, ctxPreview.width, ctxPreview.height);
ctxPreview.drawImage(img, selection.x, selection.y, selection.w, selection.h, 0, 0, canvas.width, canvas.height);
$('#avatarCanvas').attr('src', canvas.toDataURL());
$('#hdImagembase64').val(canvas.toDataURL());
See, this code get image and draw in canvas, after draw you get base64 string with canvas.toDataURL() 看,这段代码得到图像并在画布上绘制,绘制后你得到了带有canvas.toDataURL()的base64字符串
try this :D 试试这个:D
The data parameter for jQuery's $.ajax call can be a String, Object, or Array. jQuery的$ .ajax调用的data参数可以是String,Object或Array。 Based on the working example you gave, it looks like your upload script expects a parameter called "Url":
根据您提供的工作示例,您的上传脚本看起来需要一个名为“Url”的参数:
data: '{ "Url": "http://images.takungpao.com/2012/1115/20121115073901672.jpg" }'
If you wanted to pass the parameter as an Object, you might try: 如果要将参数作为Object传递,可以尝试:
data: {
Url: 'data:image/jpeg;base64,/9j/4AAQSkZJRgA..........gAooooAKKKKACiiigD//Z'
}
If you want to pass it as a String, you might try: 如果要将其作为String传递,可以尝试:
data: '{ "Url": "data:image/jpeg;base64,/9j/4AAQSkZJRgA..........gAooooAKKKKACiiigD//Z"}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.