简体   繁体   English

append 来自 img src 的图像以形成 javascript 中的数据

[英]append an image from img src to form data in javascript

I have a div and inside it there is an img tag.我有一个 div,里面有一个 img 标签。 I have inserted one image ( eg: "img/flower.png").我插入了一张图片(例如:“img/flower.png”)。 On clicking a button, is there any way to append this image to a Form Data, so as to upload them to server in node.js.?单击一个按钮,有没有办法将 append 这张图片转换为表单数据,以便将它们上传到 node.js 中的服务器。? Is it possible to upload without using form data?是否可以在不使用表单数据的情况下上传?

    <div id="sampleDiv">
    <img src="img/flower.png" id="flower" alt="Rose Flower" />
    </div>
    <input type="button" id="button" value="Upload" name="submit" />
    var form=new Form Data();
    form.append("image",document.getElementbyId("flower").src);

The FormData expects a file with parameter like in documentation: FormData 需要一个带有参数的文件,如文档中所示:

https://developer.mozilla.org/pt-BR/docs/Web/API/FormData/append https://developer.mozilla.org/pt-BR/docs/Web/API/FormData/append

You can transform the image to file using FileReader like:您可以使用 FileReader 将图像转换为文件,例如:

Loading an image to a <img> from <input file> 从 <input file> 加载图像到 <img>

https://developer.mozilla.org/pt-BR/docs/Web/API/FileReader https://developer.mozilla.org/pt-BR/docs/Web/API/FileReader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM