javascript - 一键选择文件上传
问题描述
我只需要创建一个选择文件并上传的按钮。我创建了一个选择文件的按钮,但它不会触发上传按钮中的 onclick 功能。
举个例子,我有一个 hello 按钮,它只是用来控制某些东西,但实际上它会对选定的文件做一些事情。
<div class="upload-btn-wrapper">
<button class="btn" onClick ="hello()">Upload a file</button>
<input type="file" name="myfile"/>
</div>
.upload-btn-wrapper {
position: relative;
overflow: hidden;
display: inline-block;
}
.btn {
border: 2px solid gray;
color: gray;
background-color: white;
padding: 8px 20px;
border-radius: 8px;
font-size: 20px;
font-weight: bold;
}
.upload-btn-wrapper input[type=file] {
font-size: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
function hello() {
console.log("hello");
}
codePen 链接:https ://codepen.io/anon/pen/wOmNJw
解决方案
输入覆盖按钮单击侦听器。如果您尝试对文件执行某些操作,则应将更改侦听器应用于输入并在那里运行函数。您可以使用或不使用 jquery 来执行此操作。
香草 JS(没有 Jquery)
document.getElementsByName('myfile')[0].addEventListener('change', function(){
hello(this);
});
function hello(elem) {
console.log('Hello');
console.log('File Name: ', elem.value);
}
.upload-btn-wrapper {
position: relative;
overflow: hidden;
display: inline-block;
}
.btn {
border: 2px solid gray;
color: gray;
background-color: white;
padding: 8px 20px;
border-radius: 8px;
font-size: 20px;
font-weight: bold;
}
.upload-btn-wrapper input[type=file] {
font-size: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
<div class="upload-btn-wrapper">
<button class="btn" onClick ="hello()">Upload a file</button>
<input type="file" name="myfile"/>
</div>
使用 jQuery
$('input[name="myfile"]').on('change', hello);
function hello() {
console.log("hello");
}
.upload-btn-wrapper {
position: relative;
overflow: hidden;
display: inline-block;
}
.btn {
border: 2px solid gray;
color: gray;
background-color: white;
padding: 8px 20px;
border-radius: 8px;
font-size: 20px;
font-weight: bold;
}
.upload-btn-wrapper input[type=file] {
font-size: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="upload-btn-wrapper">
<button class="btn" onClick ="hello()">Upload a file</button>
<input type="file" name="myfile"/>
</div>
编辑:反应示例
// Example class component
class InputExample extends React.Component {
handleFileChange(e){
console.log('Hello');
console.log('File Name: ', e.target.value);
}
render() {
return (
<input
id="upload"
ref="upload"
type="file"
accept="image/*"
multiple="false"
onChange={(e) => this.handleFileChange(e)}/>
);
}
}
// Render it
ReactDOM.render(
<InputExample />,
document.getElementById("react")
);
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
推荐阅读
- c# - 通过 { get; 将值传递给私有类 }
- python - 如何将二进制交叉熵用于位序列
- react-native - 从使用按钮以编程方式添加的 TextInputs 获取输入数据
- python - 如何在删除之前制作表的副本(django pre_delete)
- python - py4j.protocol.Py4JError: 调用 o66.createStream-Amazon kinesis,pyspark 时出错
- c# - .Net Core 2.2 启动中的 Services.Configuration.Replace
- react-native - 尝试播放声音时出现 RNSound.IsAndroid 错误
- javascript - v-on="..." 语法在 VueJS 中是什么意思?
- rspec - 如何在rspec中调用位于不同位置的ruby类
- python-3.x - 使用 Boto3 上传 Gzip 文件