js文件上传不用点击上传按钮自动上传

不需要点击上传按钮,选择文件后直接上传:

 

 
 
  1. <script>   
  2.     function submitform(){   
  3.         document.form01.action="upload.php";   
  4.         document.form01.submit();   
  5.     }  
  6. </script>   
  7. <form name="form01" method="post" enctype"multipart/form-data"> 
  8. <input type="hidden" name="xx" value="1" /> 
  9.     选择文件(自动上传)<input type"file" name="upfile" onchange="submitform()" />  
  10. </form>  
  11. <!--此form不能单纯的提交[上传文件]一个input,否则post不成功--> 

 



      本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/934649,如需转载请自行联系原作者




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-upload组件本身支持多文件上传,但是默认是并发上传,这样容易导致服务器压力过大。如果需要实现自动上传,可以通过设置属性`:auto-upload="false"`来禁用自动上传,然后在上传按钮点击事件中手动调用上传方法`this.$refs.upload.submit()`来实现逐个上传文件的功能。具体实现可以参考以下代码: ```html <el-upload ref="upload" :auto-upload="false" :on-success="handleSuccess" :on-error="handleError" > <el-button slot="trigger" type="primary">选取文件</el-button> <el-button style="margin-left: 10px;" type="success" @click="uploadFiles">上传文件</el-button> </el-upload> ``` ```javascript data() { return { fileList: [], // 上传的文件列表 currentIndex: 0 // 当前上传的文件索引 } }, methods: { // 上传文件 uploadFiles() { if (this.fileList.length === 0) { this.$message.warning('请先选择文件') return } this.currentIndex = 0 // 重置当前上传的文件索引 this.uploadFile(this.fileList[this.currentIndex]) // 上传第一个文件 }, // 上传单个文件 uploadFile(file) { this.$refs.upload.upload(file.raw) // 调用上传方法 }, // 上传成功回调 handleSuccess(response, file, fileList) { this.currentIndex++ // 当前上传的文件索引加1 if (this.currentIndex < this.fileList.length) { this.uploadFile(this.fileList[this.currentIndex]) // 继续上传下一个文件 } else { this.$message.success('上传成功') } }, // 上传失败回调 handleError(err, file, fileList) { this.$message.error('上传失败') } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值