原生 input 中的 “type=file“ 上传文件

news/2024/9/18 20:59:25 标签: vue.js, javascript, 前端

目标:实现文件上传功能

原型图:

HTML部分:

<div class="invoice-item">
                <div class="invoice-title">增值税专用发票</div>
                <div class="invoice-box">
                  <el-form-item label="标准模版:" class="Standard-Template">
                    <div v-show="!isEdit && !ruleForm.taxInvoiceTemplateUrl">
                      <span>待上传</span>
                    </div>
                    <div v-show="isEdit && !ruleForm.taxInvoiceTemplateUrl">
                      <input
                        ref="taxInvoiceTemplateRef"
                        type="file"
                        style="
                        opacity: 0;
                        position: absolute;
                        left: -9999px;
                        top: -9999px;
                      "
                        @change="taxInvoiceTempalteChange"
                      >
                      <i
                        class="el-icon-upload2"
                        style="color: #0075ff; font-size: 22px; cursor: pointer"
                        @click="$refs.taxInvoiceTemplateRef.click()"
                      />
                      <span
                        style="margin-left: 8px"
                        :style="{ color: errorText3 ? 'red' : '#999999' }"
                      >{{ errorText3 || "支持上传5M以内.pdf格式的文档" }}</span>
                    </div>
                    <div v-show="ruleForm.taxInvoiceTemplateUrl">
                      <i class="el-icon-document" />
                      <span>{{ ruleForm.taxInvoiceTemplateName }}</span>
                      <i
                        v-if="isEdit"
                        class="el-icon-circle-close"
                        style="cursor: pointer"
                        @click="deleteDoc('taxDigitalInvoice')"
                      />
                    </div>
                  </el-form-item>
                  <el-form-item
                    label="备注信息超长模板:"
                    class="Standard-Template"
                  >
                    <div
                      v-show="!isEdit && !ruleForm.taxInvoiceRemarkTemplateUrl"
                    >
                      <span>待上传</span>
                    </div>
                    <div
                      v-show="isEdit && !ruleForm.taxInvoiceRemarkTemplateUrl"
                    >
                      <input
                        ref="taxInvoiceRemarkTemplateRef"
                        type="file"
                        style="
                        opacity: 0;
                        position: absolute;
                        left: -9999px;
                        top: -9999px;
                      "
                        @change="taxInvoiceRemarkTemplateChange"
                      >
                      <i
                        class="el-icon-upload2"
                        style="
                        color: #0075ff;
                        font-size: 22px;
                        cursor: pointer;
                        padding-top: 10px;
                      "
                        @click="$refs.taxInvoiceRemarkTemplateRef.click()"
                      />
                      <span
                        style="margin-left: 0px; padding-top: 3px"
                        :style="{ color: errorText4 ? 'red' : '#999999' }"
                      >{{ errorText4 || "支持上传5M以内.pdf格式的文档" }}</span>
                    </div>
                    <div v-show="ruleForm.taxInvoiceRemarkTemplateUrl">
                      <i class="el-icon-document" />
                      <span>{{ ruleForm.taxInvoiceRemarkTemplateName }}</span>
                      <i
                        v-if="isEdit"
                        class="el-icon-circle-close"
                        style="cursor: pointer"
                        @click="deleteDoc('taxDigitalInvoiceRemarkInfo')"
                      />
                    </div>
                  </el-form-item>
                </div>
              </div>

JS部分:

javascript">import * as Api from '@/api/invoiceBasic'

data() {
    return {
      errorText3: '',
      errorText4: '',
      ruleForm: {
        // 数电发票基础模板

        taxInvoiceTemplateUrl: '', // 增值税 标准模板
        taxInvoiceTemplateName: '',

        taxInvoiceRemarkTemplateUrl: '',
        taxInvoiceRemarkTemplateName: '',

        sellerShowAccount: '0',
        invoiceOperator: ''
      },
      rules: {
        invoiceOperator: [
          { required: true, message: '请输入开票人', trigger: 'submit' }
        ]
      }
    }
  },

methods: {
    init() {
      this.getDetailData()
    },
    getDetailData() {
      this.pageLoading = true
      Api.ticketConfigFind()
        .then((res) => {
          const {
            taxInvoiceTemplateUrl, taxInvoiceTemplateName,
            taxInvoiceRemarkTemplateUrl, taxInvoiceRemarkTemplateName,
           
            sellerShowAccount, invoiceOperator
          } = res.data.body
          this.ruleForm.taxInvoiceTemplateUrl = taxInvoiceTemplateUrl
          this.ruleForm.taxInvoiceTemplateName = taxInvoiceTemplateName
          this.ruleForm.taxInvoiceRemarkTemplateUrl = taxInvoiceRemarkTemplateUrl
          this.ruleForm.taxInvoiceRemarkTemplateName = taxInvoiceRemarkTemplateName
          

          // this.ruleForm.taxInvoiceTemplateUrl = taxInvoiceTemplateUrl
          // this.ruleForm.taxInvoiceTemplateName = taxInvoiceTemplateName
          // this.ruleForm.ordinaryInvoiceTemplateUrl = ordinaryInvoiceTemplateUrl
          // this.ruleForm.ordinaryInvoiceTemplateName =
          //   ordinaryInvoiceTemplateName

          this.ruleForm.invoiceOperator = invoiceOperator
          this.ruleForm.sellerShowAccount = String(Number(sellerShowAccount))
        })
        .finally((_) => {
          this.pageLoading = false
        })
    },
    // 增值税专用发票 标准模板
    taxInvoiceTempalteChange(e) {
      const file = e.target.files[0]
      if (file.name.indexOf('.pdf') === -1 || file.size > 5 * 1024 * 1000) {
        this.errorText3 = '支持上传5M以内.pdf格式的文档'
        return
      }
      const f2 = new FormData()
      f2.append('file', file)
      Api.fastdfsUpload(f2).then((res) => {
        this.ruleForm.taxInvoiceTemplateName = file.name
        this.ruleForm.taxInvoiceTemplateUrl = res.data.body
      })
    },
    // 增值税专用发票 - 备注信息超长
    taxInvoiceRemarkTemplateChange(e) {
      const file = e.target.files[0]
      if (file.name.indexOf('.pdf') === -1 || file.size > 5 * 1024 * 1000) {
        this.errorText4 = '支持上传5M以内.pdf格式的文档'
        return
      }
      const f3 = new FormData()
      f3.append('file', file)
      Api.fastdfsUpload(f3).then((res) => {
        this.ruleForm.taxInvoiceRemarkTemplateName = file.name
        this.ruleForm.taxInvoiceRemarkTemplateUrl = res.data.body
      })
    },
},

CSS部分:

.invoice-item {

      float: left;

      width: 430px;

      height: 140px;

      // height: 126px;

      border-radius: 12px;

      margin-top: 12px;

      margin-bottom: 22px;

      background: linear-gradient(to right, #cfe8ff, #f8fcff);

      .invoice-box {

        width: 100%;

        .remark-Template {

          /deep/ .el-form-item {

            margin-bottom: 0px;

          }

        }

        /deep/ .el-form-item__label {

          width: fit-content !important;

          padding-left: 20px;

          font-weight: 600;

          font-size: 12px;

          color: #101010;

        }

        /deep/ .el-form-item__content {

          text-align: right;

          padding-right: 20px;

          margin-left: 180px !important;

        }

      }

    }

    .invoice-title {

      font-weight: 600;

      font-size: 16px;

      color: #0075ff;

      padding: 20px;

      padding-bottom: 8px;

    }

效果:


http://www.niftyadmin.cn/n/5664514.html

相关文章

《MmAP : Multi-Modal Alignment Prompt for Cross-Domain Multi-Task Learning》中文校对版

系列论文研读目录 文章目录 系列论文研读目录摘要1 引言2 相关工作3 方法3.1对比图像预训练3.2 多模式对齐提示3.3 多任务提示学习框架 4 实验4.1基准设置4.2实验结果4.3消融研究 5、结论 摘要 多任务学习&#xff08;Multi-Task Learning&#xff0c;MTL&#xff09;是为了同…

如何打造出强悍的谷歌搜索关键词优化方案揭密

搭建一个成功的关键词优化规划是促进网站在谷歌搜索引擎中取得更强曝光和流量重要。本文将为你揭露七个秘笈&#xff0c;帮助自己打造出强悍的谷歌搜索关键词优化方案。1.目标制定在进行优化关键词以前&#xff0c;必须明确自己的目标。你希望用谷歌搜索引擎获得更多浏览量和访…

【计算机网络】第一章

目录 一、计算机网络的各种定义 二、计算机网络的发展 三、计算机网络的功能 四、计算机网络的分类 五、Internet的组成 六、交换 一、计算机网络的各种定义 Internet&#xff1a;因特网&#xff08;外国资源&#xff09; internet&#xff1a;互联网络&#xff08;两个或…

理解AAC和Opus的编码与解码流程

理解AAC和Opus的编码与解码流程及其在Android中的实现,对于音频开发非常重要。下面,我将详细解释这两种编码格式的原理、流程,并结合具体代码示例,帮助你在Android项目中合理地设计和使用它们。 一、AAC(Advanced Audio Coding) 1. AAC的原理与流程 AAC是一种有损音频压…

8--SpringBoot原理分析、注解-详解(面试高频提问点)

目录 SpringBootApplication 1.元注解 --->元注解 Target Retention Documented Inherited 2.SpringBootConfiguration Configuration Component Indexed 3.EnableAutoConfiguration&#xff08;自动配置核心注解&#xff09; 4.ComponentScan Conditional Co…

ARM base instruction -- blr

BLR Branch with Link to Register calls a subroutine at an address in a register, setting register X30 to PC4. 带寄存器链接的分支在寄存器中的某个地址调用一个子程序&#xff0c;将寄存器 X30 (lr) 设置为 PC4。 BLR <Xn> BLR 跳转到reg内容地址&#xff0c;…

10.第二阶段x86游戏实战2-反编译自己的程序加深堆栈的理解

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 本次游戏没法给 内容参考于&#xff1a;微尘网络安全 工具下载&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1rEEJnt85npn7N38Ai0_F2Q?pwd6tw3 提…

Vue学习记录之一(介绍及脚手架的使用)

一、背景知识介绍 1、构建工具介绍 Vite, Webpack,Rollup, Parce 构建工具优点缺点Vite- 快速启动&#xff0c;秒级热更新&#xff0c;更快的构建速度&#xff0c;更好的开发体验&#xff1b;- 支持 Vue3 和 ES modules 的原生特性&#xff0c;轻松实现按需加载。- 对于单页…