将dataUrl转换为blob并通过ajax提交
Convert dataUrl to blob and submit through ajax
我使用的是imgly image cropper插件,该插件针对我的应用程序进行了轻微修改。它目前将图像转换为dataUrl
,并将图像作为base64图像输出,我可以将其保存为jpeg。我正在将这里的dataURItoBlob
函数适配到我的应用程序中,这样我就可以将图像发布到API端点。到目前为止,我有以下内容,但我不确定如何将最终图像附加到xhr.open('POST', '/', true);
renderButton.click(function (event) {
var dataUrl =
imgly.renderToDataURL("image/jpeg", { size: "1200" }, function (err, dataUrl) {
//Convert DataURL to Blob to send over Ajax
function dataURItoBlob(dataUrl) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataUrl.split(',')[1]);
// separate out the mime component
var mimeString = dataUrl.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
//var bb = new BlobBuilder();
//bb.append(ab);
//return bb.getBlob(mimeString);
}
var blob = dataURItoBlob(dataUrl);
var fd = new FormData(document.forms[0]);
var xhr = new XMLHttpRequest();
fd.append("myFile", blob);
xhr.open('POST', '/', true);
xhr.send(fd);
//Appends generated dataUrl to a div
var image = $("<img><br>").attr({
src: dataUrl
});
//Remove button
image.appendTo($(".result"))
$button = $('<button class="btn btn-default remove">')
.text('Remove Image')
.on('click', function () {
image.remove();
$(this).remove();
return false;
});
$button.appendTo($(".result"));
});
});
});
更新的
"假设我保持应用程序的相同形式,我正试图弄清楚如何将数据URL输入post函数。"
试试看,在第15-103行http://jsfiddle.net/mattography/Lgduvce1/6/
var blob; // declare `blob`
// As soon as the user selects a file...
fileInput.addEventListener("change", function (event) {
var file; // declare `file`
var fileToBlob = event.target.files[0];
blob = new Blob([fileToBlob], {"type":fileToBlob.type});
// do stuff with blob
console.log(blob);
// Find the selected file
if(event.target.files) {
file = event.target.files[0];
} else {
file = event.target.value;
}
// Use FileReader to turn the selected
// file into a data url. ImglyKit needs
// a data url or an image
var reader = new FileReader();
reader.onload = (function(file) {
return function (e) {
data = e.target.result;
// Run ImglyKit with the selected file
try {
imgly.run(data);
} catch (e) {
if(e.name == "NoSupportError") {
alert("Your browser does not support canvas.");
} else if(e.name == "InvalidError") {
alert("The given file is not an image");
}
}
};
})(file);
reader.readAsDataURL(file);
});
// As soon as the user clicks the render button...
// Listen for "Render final image" click
renderButton.click(function (event) {
var dataUrl;
imgly.renderToDataURL("image/jpeg", { size: "1200" }
, function (err, dataUrl) {
// `dataUrl` now contains a resized rendered image with
// a width of 300 pixels while keeping the ratio
// Convert DataURL to Blob to send over Ajax
// function dataURItoBlob(dataUrl) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs
// - see SO answer #6850276 for code that does this
// var byteString = atob(dataUrl.split(',')[1]);
// separate out the mime component
// var mimeString = dataUrl.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
// var ab = new ArrayBuffer(byteString.length);
// var ia = new Uint8Array(ab);
// for (var i = 0; i < byteString.length; i++) {
// ia[i] = byteString.charCodeAt(i);
// }
// write the ArrayBuffer to a blob, and you're done
// var bb = new BlobBuilder();
// bb.append(ab);
// return bb.getBlob(mimeString);
// }
var _data = dataUrl.split(/,/)[1];
// var fd = new FormData(document.forms[0]);
var xhr = new XMLHttpRequest();
function success(response) {
if (response.target.readyState === 4) {
var data = JSON.parse(response.target.response);
var image = "data:" + data.type + ";base64," + data.file;
console.log(image); // `data URI` of resized image
}
}
xhr.onload = success;
// fd.append("myFile", blob);
xhr.open("POST", "/echo/json/", true);
xhr.send("json=" + encodeURIComponent(
JSON.stringify({"file": _data,"type":blob.type})
)
);
另请参阅Handling_the_upload_process_for_a_file
相关文章:
- 表单不会通过ajax提交
- 使用Ajax提交表单
- 如何创建动态ajax提交表单
- 如何在使用ajax提交的codeigniter中使用form_validation验证表单数据
- Ajax提交多值
- jquery通过ajax验证电子邮件,然后再通过ajax提交表单
- 检测ajax提交
- 带有两个变量函数的Ajax提交表单
- 从AJAX提交中拆分数组$_FILES
- 将dataUrl转换为blob并通过ajax提交
- 带有前导散列字符的ajax提交的目的是什么
- Ruby On Rails Ajax 提交提交两次
- AJAX 表单不是 AJAX 提交
- Ajax 提交表单有时有效,有时直接重定向到操作网址
- 在使用 jQuery 验证插件进行验证后,使用 AJAX 提交表单
- 如何使用Jquery AJAX提交多个表单
- 使用ajax提交邮件表单的正确方法?-没有JQuery
- 如何使js成为通过ajax提交的rails表单的格式
- 在jquery停止事件时通过ajax提交表单
- 在AJAX提交之前使用jQuery进行多重下拉选择验证