vue项目通过点击文字上传html文件,查看html文件

上传html文件

解决思路:新建一个上传组件,将它挪到页面之外。当点击文字时,手动触发上传组件,打开上传文件框。

<template>
  <BasicTable @register="registerTable">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <TableAction :actions="[
          {
          label: '上传',
          onClick: handleUploadClick.bind(null, record),
          },
          {
          label: '上传记录',
          onClick: handleCheck.bind(null, record),
          }
          ]" />
      </template>
    </template>
  </BasicTable>

  <!-- 页面外的上传组件,点击文字后触发 -->
  <a-upload ref="uploadDom" :show-upload-list="false" :customRequest="handleUpload" class="record-uploader">
    <a-button type="primary">上传已填写的模板</a-button>
  </a-upload>
</template>

<script lang="ts">
  import { defineComponent, onMounted, ref, unref, onUpdated } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { message } from 'ant-design-vue';
  import { grtBannerList } from "/@/api/b2b/serviceManagement";
  import { getBasicColumns } from './index.data';
  import { uploadHtml } from '/@/api/b2b/systemSet';
  import {
    Button,
    Upload,
  } from 'ant-design-vue';

  export default defineComponent({
    name: 'agreementSetPage',
    components: {
      BasicTable,
      AButton: Button,
      AUpload: Upload,
      TableAction,
    },
    setup() {
      const [registerTable, { setShowPagination }] = useTable({
        // api: grtBannerList,
        dataSource: tableData,
        columns: getBasicColumns(),
        useSearchForm: false,
        tableSetting: {
          redo: false,
          size: false,
        },
        showIndexColumn: false,
        rowKey: 'id',
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action'
        },
      });

      const tabActiveKey = ref<string | number>('1');
      const uploadDom = ref(null)
      // 手动触发上传
      function handleUploadClick(record: any) {
        tabActiveKey.value = record.id
        uploadDom.value.$el.querySelector('button').click()
      }
      // 上传方法
      function handleUpload(file) {
        if (file.file.type !== "text/html") {
          message.warning('请上传html格式的文件')
          return
        }
        let formData = new FormData();
        formData.append('file', file.file);
        formData.append('type', unref(tabActiveKey));
        uploadHtml(formData).then(async (res) => {
          message.success('上传成功')
        });
      }
      // 查看上传记录
      function handleCheck(record) {
        tabActiveKey.value = record.id
        title.value = `上传记录(${record.agreementType})`
        let data = { id: record.id }
        openModalOperateRecord(true, data);
      }

      return {
        registerTable,
        registerOperateRecord,
        isShow,
        handleUploadClick,
        handleCheck,
        bannerDate,
        handleUpload,
        tabActiveKey,
        uploadDom,
        tabActiveKey,
        title
      };
    }
  })
</script>

查看html文件

解决思路:将接口返回的html完整文件代码缓存到本地,然后读取本地保存的html数据,使用新窗口打开,就能查看html文件。

<template>
  <div>
    <BasicModal v-bind="$attrs" @register="registerModal" :title="title" okText="确定" cancelText="取消"
      :canFullscreen="false" :footer="null" @visible-change="handleVisibleChange" @ok="handleOk"
      wrapClassName="recordModalWrap" :useWrapper="false" :width="630" :top="19">
      <BasicTable @register="registerRecordTable">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <TableAction :actions="[
      {
        label: '查看',
        onClick: handleCheck.bind(null, record),
      },
    ]" />
          </template>
        </template>
      </BasicTable>
    </BasicModal>

  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicTable, useTable, BasicColumn, TableAction } from '/@/components/Table';
import { GetOperateRecordPage } from '/@/api/b2b/systemSet';

const recordColumns: BasicColumn[] = [
  {
    title: '上传时间',
    dataIndex: 'createTime',
  },
];
export default defineComponent({
  name: 'SystemSetOperateRecord',
  components: { BasicTable, BasicModal, TableAction },
  props: {
    tabActiveKey: { type: [String, Number], require: true },
    title: { type: String, default: '' }
  },
  emits: ['success', 'register'],
  setup(props, { emit }) {
    console.log(666, props);
    const modelRef = ref({});
    const [registerRecordTable, { reload }] = useTable({
      api: GetOperateRecordPage,
      columns: recordColumns,
      useSearchForm: false,
      showTableSetting: false,
      showIndexColumn: false,
      striped: false,
      rowKey: 'id',
      scroll: { y: 330 },
      actionColumn: {
        width: 120,
        title: '操作',
        dataIndex: 'action'
      },
      fetchSetting: { listField: 'records', totalField: 'total' },
      beforeFetch: (params) => {
        let newParams = {
          ...params,
          type: props.tabActiveKey,
        };
        return newParams;
      },
    });

    const [registerModal] = useModalInner((data) => {
      reload()
    });

    function handleCheck(record: any) {
      // 读取本地保存的html数据,使用新窗口打开
      window.localStorage.removeItem('callbackHTML');
      window.localStorage.setItem('callbackHTML', record.content)
      let newWin = window.open('')
      newWin.document.write(localStorage.getItem('callbackHTML'))
      newWin.document.close() // 关闭输出流
    }

    function handleVisibleChange(v) { }

    async function handleOk() { }
    return {
      registerModal,
      model: modelRef,
      handleCheck,
      registerRecordTable,
      handleVisibleChange,
      handleOk,
    };
  },
});
</script>

<style scoped lang="less">
:deep(.ant-table-wrapper) {
  padding: 0px !important;
}

:deep(.ant-spin-container) {
  height: 442px;
}

:deep(.scroll-container) {
  padding: 0 !important;
}

:deep(.ant-pagination-options-quick-jumper) {
  display: none;
}

:deep(.ant-table-body) {
  height: 330px !important;
  max-height: 696px !important;
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue中使用Element UI的表单和拖拽文件上传功能,你需要结合Element UI的Upload组件和拖放API来实现。以下是一个示例: 1. 首先,确保已经安装并引入了Element UI和相应的样式。 2. 在Vue组件中,使用Element UI的Upload组件,并添加一个拖拽区域: ```html <template> <el-upload action="/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :auto-upload="false" drag multiple > <i class="el-icon-upload"></i> <div class="el-upload__text">将文件拖到此处,或点击上传</div> </el-upload> </template> ``` 在这个示例中,我们使用了`el-upload`组件,并启用了拖拽功能(`drag`)和多文件上传(`multiple`)。你需要根据实际需求,设置`action`、`on-success`、`before-upload`和其他相关属性。 3. 在Vue组件的`methods`中定义处理上传成功和上传前的方法: ```javascript <script> export default { methods: { handleSuccess(response, file, fileList) { // 处理上传成功的逻辑 // response为服务器返回的响应数据 }, beforeUpload(file) { // 处理上传前的逻辑 // 返回false可以阻止文件上传 // 返回Promise可以进行异步操作,比如文件校验等 } } }; </script> ``` 在上述示例中,`handleSuccess`方法会在文件上传成功后被调用,你可以在此方法中处理上传成功后的逻辑。`beforeUpload`方法会在文件上传前被调用,你可以在此方法中进行上传前的逻辑处理。你可以根据需要修改这两个方法的具体实现。 4. 在CSS中为拖拽区域添加样式: ```css <style> .el-upload-dragger { border: 2px dashed #ccc; text-align: center; padding: 20px; } </style> ``` 这样,你就可以在Vue中使用Element UI的表单和拖拽文件上传功能了。拖拽区域会显示一个上传图标和提示文字,用户可以将文件拖拽到该区域进行上传上传文件会发送到指定的服务器地址,并在上传成功后调用`handleSuccess`方法进行处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值