[英]Input tag type file
How to import file to the input tag (default.jpeg for example) if user does not select the file, I want to get the file from the input tag in JS and pass it to the server then get it and load. 如果用户未选择文件,如何将文件导入输入标签 (例如,default.jpeg),我想从JS中的输入标签获取文件并将其传递到服务器,然后获取并加载。 Now It works, but I can only get the file when user select itself, but when user does not select the file, I got error on the server, because the method want the file and that is why I want to load the default file to the input tag before user select any.
现在它可以工作,但是我只能在用户选择自身时获取文件,但是当用户不选择文件时,服务器上会出现错误,因为该方法需要文件,这就是为什么要将默认文件加载到用户选择任何内容之前输入标签。
做这个:
document.querySelector("input[type='file']").value = "default.jpeg";
You can't. 你不能
The only way to set the value of a file input is by the user to select a file. 设置文件输入值的唯一方法是用户选择文件。
This is done for security reasons. 出于安全原因这样做。 Otherwise, you would be able to create a Javascript that automatically uploads a specific file from the client's computer.
否则,您将能够创建自动从客户端计算机上传特定文件的Javascript。
But if you want to do it forcefully you can do it like that. 但是,如果您想强制执行此操作,则可以这样做。
<!DOCTYPE html>
<html>
<body>
Name: <input type="file" id="myText" value="Mickey">
<p>Click the button to display the default value of the text field.</p>
<button type="button" onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myText").defaultValue="default.jpeg";
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Hope it will help you in your case. 希望它对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.