vue/html input 读取 json数据

5 篇文章 0 订阅
<template>
  <div class="p-content">
    <div>
      <input @change="handleGetFile" type="file" id="file" />
    </div>
  </div>
  <!-- <HelloWorld msg="Hello Vue 3 + TypeScript + Vite" /> -->
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component';
import HelloWorld from './components/HelloWorld.vue';
import {Room} from './types/room'


@Options({
  components: {
    HelloWorld,
  },
})
export default class App extends Vue {
  list: Room[] = [];

  handleGetFile(file: any) {
    const dom = document.getElementById('file') as HTMLInputElement;
    if (dom && dom.files) {
      console.log(dom.files);
      const file = dom.files[0];
      const types = file.name.split('.')[1];
      const fileType = ['json'].some(
        (item) => item === types
      );
      if (!fileType) {
        alert('格式错误,请上传excel类型文件');
        return;
      }

      this.handleReadFile(file)

    }
  }

  handleReadFile(file: File) {
    const reader = new FileReader();
    reader.readAsText(file);
    reader.onload = (thisFile)=>{
      if(thisFile.target && thisFile.target.result) {
        const result = JSON.parse(thisFile.target.result.toString())
        console.log(result)
      }
    }
  }


}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VUE中导入Excel并读取数据后传递到C#后台接收数据的代码演示可以按以下步骤进行: 1. 首先,在VUE中使用第三方库如xlsx来实现导入Excel功能。可以使用npm安装xlsx库,并在代码中引入相关模块。 2. 在VUE中创建一个上传文件的组件,用于选择和上传Excel文件。可以使用input标签或者其他UI库来实现文件选择和上传的功能。 3. 在VUE的上传文件组件中,当选择完Excel文件后,通过xlsx库来读取Excel文件的数据。具体的读取过程可以参考xlsx的文档,使用相关的API来解析Excel文件。 4. 在VUE中将读取到的Excel数据通过HTTP请求发送到C#后台。可以使用axios库或者其他HTTP请求库来发送POST请求,并将读取到的Excel数据作为请求的payload。 5. 在C#后台中创建一个接口来接收VUE发送的HTTP请求,并解析请求的数据。可以使用ASP.NET Core的Web API来创建接口,并使用[FromBody]注解来接收VUE发送的JSON数据。 6. 在C#后台接收到数据后,可以对数据进行处理,例如将数据保存到数据库或者进行其他的业务逻辑操作。 下面是一个简单的代码示例,演示了VUE导入Excel并传递数据到C#后台接收的过程: 在VUE组件中的代码: ```javascript <template> <div> <input type="file" @change="handleFileUpload" accept=".xlsx" /> <button @click="uploadData">上传</button> </div> </template> <script> import axios from 'axios'; import XLSX from 'xlsx'; export default { methods: { handleFileUpload(event) { const file = event.target.files object data) { // 在这里处理接收到的数据,例如保存到数据库或进行其他的业务逻辑操作 // 返回处理结果给VUE前端 return Ok("Data received and processed successfully"); } } ``` 请注意,以上代码仅为示例,具体的实现需要根据项目的实际需求进行适当的调整和修改。同时,还需要处理一些异常情况,例如文件上传失败或数据解析错误等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java开发常见专业术语](https://blog.csdn.net/weixin_42408447/article/details/116587614)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Java面试宝典Beta5.0](https://blog.csdn.net/cillent_boy/article/details/87348819)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值