我正在尝试创建一个浏览器对话框,让用户将图像上传到我的网页.
我知道我必须使用< input type =“file”>但是,当用户接受单击按钮时出现的提示时,我不知道在哪里(或更好,何时)检索信息.
在我的情况下,当您单击按钮时,会出现提示,我通过Javascript处理此事件.
这是我的代码:
window.onload = function(){
var buttonFile = document.getElementById("buttonFile");
var file = document.getElementById("file");
buttonFile.onclick = function(){
document.getElementById("file").click();
}
var files = document.getElementById('file').files[0]; //I do not know where to put this line
alert(files);
};
#file{
display: none;
}
Upload file
当然,现在正在检索未定义因为我试图检索它,无论提示是否出现.如果我将该行放在按钮的onclick事件中,它还没有信息.我还尝试为该文件创建一个onclick事件,但它也不检索信息,因为它不知道何时被接受.
所以我在这里有一些问题:
>我应该在哪里放置这一行来获取我上传的图片的价值?
>由于旧浏览器不支持输入过滤器,我是否也应该在服务器端检查它,对吧?
>如果我想在服务器端(PHP)检查它,我是否要将其链接到表单?
提前致谢!