如何通过内容类型为“image/jpeg”的 AJAX 调用将画布图像从 GUI 保存到后端
How to save a canvas image from GUI to back-end via AJAX call with content-type "image/jpeg"?
canvas
如何通过AJAX
接受content type "image/jpeg"
克服错误" jquery.js:8453 Uncaught TypeError: Illegal invocation
.HTML
<canvas id="myImage"></canvas> // Canvas loaded with image
HTML
<canvas id="myImage"></canvas> // Canvas loaded with image
.JS
var can = $("#myImage");
var blob = this.dataURItoBlob(can.toDataURL("image/jpeg"));
var postPromise = $.ajax({
type: "PUT",
url: 'URL_to_save_the_image',//API URL to save image
data: blob, //data should not be transformed to query string
processData: false, // Important to overcome "jquery.js:8453 Uncaught TypeError: Illegal invocation" due to transformation to query string
contentType: 'image/jpeg'
});
postPromise.done(function(data, textStatus, jqXHR) {
//Success code goes here
});
postPromise.fail(function(jqXHR, textStatus, errorThrown) {
//Failure code goes here
});
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {
type: mimeString
});
}
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 角度:在ng重复上切换图像
- 画廊图像未显示
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- Javascript排序的图像弹出窗口..可以't单独弹出
- 画布数据到图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使图像在单击时展开到不大于浏览器
- Html页面上的多个Base64图像和平滑加载
- UIAutomation放大图像
- JavaScript 使用 url 上传图像(图像已经在互联网上,不同的域)
- 使用JavaScript更改背景图像图像以进行图像幻灯片
- 尝试为石头剪刀布游戏显示图像而不是字母
- 上传前无法显示图像图像
- 编辑图像/图像详细信息-替换按钮缺失
- 在缩略图上更改滑块图像.图像改变一秒钟,然后恢复
- 在jQuery中移动图像(图像裁剪)
- 而不是提示从图像中选择石头,布,剪刀等游戏