猿问

input 怎么自动上传图片

我要上传一个图片,如果用input type= file,怎么才能做到选择图片,点击确定之后,就自动把这个图片上传呢

繁花不似锦
浏览 357回答 1
1回答

一只斗牛犬

<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; &nbsp; &nbsp; <title></title>&nbsp; &nbsp; </head><body>&nbsp; &nbsp; <p>&nbsp; &nbsp; <label>请选择一个图像文件:</label>&nbsp; &nbsp; <input type="file" id="file_input" /> 选择图片的input按钮&nbsp; &nbsp; </p></body></html><script type="text/javascript">&nbsp; &nbsp; var input = document.getElementById("file_input");&nbsp; &nbsp; input.addEventListener('change', readFile, false);&nbsp; &nbsp; function readFile() {&nbsp; &nbsp; var file = this.files[0]; //获取file对象&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // 这里写一个ajax把通过post把this.files[0]提交到后台就可以了。}</script>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答