如何提交html5 canvas作为表单帖子的一部分? [英] How to submit html5 canvas as part of form post?

查看:136
本文介绍了如何提交html5 canvas作为表单帖子的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将图像数据从canvas标签流式传输到node.js服务器。我自己可以处理服务器端代码,但是如何从画布提交数据呢?我希望有一个涉及多部分表单数据的建议,因为我想要流式传输数据,因为我期待大约50 MB左右的图像。如果我尝试一次性发布数据,则会导致客户端浏览器崩溃。

I'm looking to stream the image data from a canvas tag to a node.js server. I can handle the server-side code myself, but how can I submit the data from a canvas? I'm hoping for a suggestion involving multipart form data because I want to stream the data, since I'm expecting images in the ballpark of 50 MB or so. If I try to post the data all at once, it tends to crash the client's browser.

推荐答案

您可以使用 FormData 模拟正常multipart / form-data文件提交:

You can use FormData to emulate a normal "multipart/form-data" file submit:

canvas.toBlob( function(blob) {

    var formData = new FormData();

    formData.append("image_file", blob );
    var xhr = new XMLHttpRequest;
    xhr.open( "POST", "abc.php" );
    xhr.send(formData);

}, "image/png");

画布方法 .toBlob 指定但未实现,你可以使用polyfill
,例如画布 - to-blob.js

The canvas method .toBlob is specified but not implemented, you can use a polyfill such as canvas-to-blob.js

这篇关于如何提交html5 canvas作为表单帖子的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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