js点击按钮上传文件

在这里插入图片描述

vue语法

<template>
    <div  style="width: 152px;">
        <input id="file" ref="file" class="filepath" @change="changepic()" type="file" />
        <el-button size="small" type="primary" @click="upLoadName()">点击上传</el-button>
    </div>
</template>

<script>
export default {
    components: {},
    data() {
        return {

        };
    },
    computed: {},
    watch: {},
    methods: {
        changepic() {
            var f = document.getElementById('file').files[0];
            var form = {};
            form = new FormData();
            form.append('file', f);
            this.$axios.post('https://file.xxx.com:9500/API/xxx/UpLoadFiles', form).then((res) => {
                console.log(res, 'res');
            })
        },
        upLoadName() {
            this.$refs.file.click();

        },
    },
    created() {
    },
    mounted() {
    },
    beforeCreate() { },
    beforeMount() { },
    beforeUpdate() { },
    updated() { },
    beforeDestroy() { },
    destroyed() { },
    activated() { },
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类

</style>
原生方法
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=0.5, maximum-scale=2.0">
  <link rel="shortcut icon" type="image/x-icon" id="linkHref" href="./logo/logo.ico">
  <title id="checkTitle">标题</title>


</head>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<body>
  <noscript>
    <strong></strong>
  </noscript>
  <div id="app"></div>
  <input id="file"  class="filepath" onchange="changepic()" type="file"  />
  <button onclick="upLoadName()">点击上传</button>

</body>

<script>
  function upLoadName() {
    document.getElementById("file").click()
  }
  function changepic() {
    var f = document.getElementById('file').files[0];
    var form = {};
    form = new FormData();
    form.append('file', f);
    axios.post('https://file.xxx.com:9500/API/xxx/UpLoadFiles', form).then((res) => {
      
      let imgurl = 'https://file.xxx.com:9501/Files/' + res.data[0].Name
    })
  }






</script>

</html>
  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值