从SRC的值在标签获取的图像,并通过javascript
问题描述:
我有一个HTML形式,其允许用户上载的图像,这样将它提交给:从SRC的值在<img>标签获取的图像,并通过javascript
<form method="POST" action="/somewhere">
<input type="file" />
<input type="submit" />
</form>
我的问题是:
i)如何选择HTML(img)标记中的图像,并在type =“file”的输入元素上使用javascript进行选择并提交表单,而无需用户填写并单击什么?
或者为了理解我的问题,假设你在value =“”中嵌入了一个文件。 将帖子
答
这将取决于这个地方的图像从何而来。如果从不同的出身,你是有点卡住,否则,
fetch(img.src) // fetch the image resource
.then(r => r.blob()) // as blob
.then(blob =>{
cont form = new FormData(); // create a FormData
form.append('file', blob, filename); // add the blob, I let you define the filename
// post it to your server
return fetch('/somewhere', {method:'POST', body:form});
}).then(r => console.log('sent'));
答
出于安全原因,你不能设置默认输入[类型=“文件”]
我可以通过HTTP请求发送图像吗? – teachm3