2017-09-06 87 views
0

我有一个HTML形式,其允许用户上载的图像,这样将它提交给从SRC的值在<img>标签获取的图像,并通过javascript

<form method="POST" action="/somewhere"> 
    <input type="file" /> 
    <input type="submit" /> 
    </form> 

我的问题是:

i)如何选择HTML(img)标记中的图像,并在type =“file”的输入元素上使用javascript进行选择并提交表单,而无需用户填写并单击什么?

或者为了理解我的问题,假设你在value =“”中嵌入了一个文件。 将帖子

回答

0

这将取决于这个地方的图像从何而来。如果从不同的出身,你是有点卡住,否则,

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')); 

这里我使用ES6语法和fetch API,但同样可以用XHR来完成。
关于FormData的文档。

0

出于安全原因,你不能设置默认输入[类型=“文件”]

+0

我可以通过HTTP请求发送图像吗? – teachm3

相关问题