在 formdata 中附加创建的图像文件 [英] Append created image file in formdata

查看:25
本文介绍了在 formdata 中附加创建的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用画布创建了一个图像.我想在表单数据中附加确切的图像文件而不是 url.这是我的代码.

I've created an image using canvas. I want to append the exact image file in the form data and not the url. This is my code.

<video></video>

<button type="button" onclick="turnOn()">turn on cam</button>
<button type="button" onclick="takeSnapshot()">capture image</button>


<script>
    function turnOn() {
         document.getElementsByTagName('video')[0].play();

         var video = document.querySelector('video')
          , canvas;

        if (navigator.mediaDevices) {
           navigator.mediaDevices.getUserMedia({video: true})
            .then(function(stream) {
              video.src = window.URL.createObjectURL(stream);
            })
            .catch(function(error) {
              document.body.textContent = 'Could not access the camera. Error: ' + error.name + " " + error.message;
            });
        }
    }

    function takeSnapshot() {
        var video = document.querySelector('video')
          , canvas;

        var img = document.querySelector('img') || document.createElement('img');
        var context;
        var width = video.offsetWidth
            , height = video.offsetHeight;

        canvas = canvas || document.createElement('canvas');
        canvas.width = width;
        canvas.height = height;

        context = canvas.getContext('2d');
        context.drawImage(video, 0, 0, width, height);

        img.src = canvas.toDataURL('image/png');
        document.body.appendChild(img);

        var fd = new FormData(document.forms[0]);
        fd.append("image", img);

        $.ajax({
            type: "POST",
            enctype: 'multipart/form-data',
            url: "/api/file/upload",
            data: fd,
            processData: false,
            contentType: false,
            cache: false,
            success: (data) => {
                alert("yes");
            },
            error: function(xhr, status, error) {
                  alert(xhr.responseText);
                }
        });
    }

</script>

它附加了 blob 文件" ...",我希望它附加准确的图像文件.我之前没有使用过这个,似乎无法理解其他教程.希望你能帮助我.非常感谢!!!

It's appending the blob file "����..." And I want it to append the exact image file. I haven't used this before and can't seem to understand other tutorials. Hoping you could help me. Thank you so much!!!

我已经编辑了代码并尝试附加我附加在身体上的相同图像.但它不起作用..

I already edited the code and tried to append the same image I'm appending on the body. But it's not working..

推荐答案

您可以尝试将 canvas.toDataURL('image/png') 返回的基于 64 位编码的内联图像转换为相同类型使用 document.getElementById 时从 <input id="file" type="file"></input> 元素获得的 File 对象("file").files[0] 并将其附加到 fd.

You might try converting the based 64 encoded inline image returned by canvas.toDataURL('image/png') to the same kind of File object you get from an <input id="file" type="file"></input> element when using document.getElementById("file").files[0] and append that to fd.

尝试一下,改变一下

var fd = new FormData(document.forms[0]);
fd.append("image", img);

$.ajax({
    type: "POST",
    enctype: 'multipart/form-data',
    url: "/api/file/upload",
    data: fd,
    processData: false,
    contentType: false,
    cache: false,
    success: (data) = > {
        alert("yes");
    },
    error: function(xhr, status, error) {
        alert(xhr.responseText);
    }
});

fetch(img.src)
    .then(res => res.blob())
    .then(blob => {
        const file = new File([blob], "capture.png", {
            type: 'image/png'
        });
        var fd = new FormData();
        fd.append("image", file);
        $.ajax({
            type: "POST",
            enctype: 'multipart/form-data',
            url: "/api/file/upload",
            data: fd,
            processData: false,
            contentType: false,
            cache: false,
            success: (data) => {
                alert("yes");
            },
            error: function(xhr, status, error) {
                alert(xhr.responseText);
            }
        });
    });

这篇关于在 formdata 中附加创建的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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