简体   繁体   English

如何通过 Javascript 上传图片?

[英]How to upload image via Javascript?

I am trying to upload an image to an input[type='file'] of another page.我正在尝试将图像上传到另一个页面的 input[type='file']。

Here is what I have so far:这是我到目前为止所拥有的:

 const dT = new ClipboardEvent('').clipboardData || new DataTransfer();
 dT.items.add(new File(result.products[pointer][3], 'product_image.png'));
 document.querySelector('input[class="mkhogb32"]').files = dT.files;
 console.log(dT);
 console.log(document.querySelector('input[class="mkhogb32"]').files);

The code goes through and creates a file and adds it to the inputs files, however, the image never actually gets uploaded to the page:代码通过并创建一个文件并将其添加到输入文件中,但是,图像实际上从未上传到页面:

Terminal of page页面终端

The image above shows the files of the input after my function ran, showing the function went through.上图显示了我的 function 运行后的输入文件,显示了 function 经过。 However, on this particular page, when an image is uploaded the traditional way off picking a file off your desktop or drag and drop it changes the css, as it displays the image.然而,在这个特定页面上,当图像以传统方式上传时,从桌面上选择文件或拖放它会更改 css,因为它会显示图像。

How can I get my injected file to trigger that same reaction?如何让我注入的文件触发相同的反应?

The result.products[pointer][3] refers to an image src scraped from a previous page, how can I make the injected file contain this image? result.products[pointer][3]指的是从上一页抓取的图像src ,如何使注入的文件包含该图像?

In this example I'm picking a local image.在这个例子中,我选择了一个本地图像。 The FileReader object will read the file as a data URL and when ready ('load') it will insert the data URL into the src attribute if the image. FileReader object会将文件作为数据 URL 读取,并且当准备好(“加载”)时,它会将数据 URL 插入到 image.src 属性中

 var reader1 = new FileReader(); reader1.addEventListener('load', e => { document.querySelector('#img').src = e.target.result; }); document.addEventListener('DOMContentLoaded', e => { document.forms.pickfile.file.addEventListener('change', e => { reader1.readAsDataURL(e.target.files[0]); }); });
 <form name="pickfile"> <input name="file" type="file" /> </form> <img id="img" />

I don't think that it is possible to add a file to an input[type="file"] element using JavaScript, but you can grab the formdata from a form and then add your own data to formdata.我认为不可能使用 JavaScript 将文件添加到input[type="file"]元素,但您可以从表单中获取 formdata,然后将自己的数据添加到 formdata。

In the example: When the user click on the submit I "copy" the formdata from the form (OK, there is no data in this example, but still...).在示例中:当用户单击提交时,我从表单中“复制”表单数据(好的,此示例中没有数据,但仍然......)。 I create a file object (maybe you already have a file object or a list of file objects) and set that as an object in formData.我创建了一个文件 object(也许您已经有一个文件 object 或文件对象列表)并将其设置为 formData 中的 object。 Lastly I post the formData as if it was a plain POST submit from the form.最后,我发布 formData,就好像它是来自表单的普通 POST 提交一样。

Maybe this can help...也许这可以帮助...

 document.forms.pickfile.addEventListener('submit', e => { e.preventDefault(); let svg = ['<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"><rect fill="blue" width="200" height="200" x="0" y="0"/></svg>']; let file = new File(svg, 'example.svg', {type: 'image/svg'}); let formData = new FormData(e.target); formData.set('file', file, 'example.svg'); fetch('/', { method: 'POST', body: formData }).then(response => response.json()).then(data => console.log(data)); });
 <form name="pickfile"> <input name="file" type="file" /> <button>Send</button> </form>

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

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