当前位置: 首页 > news >正文

深圳建设资格注册中心网站seo的培训网站哪里好

深圳建设资格注册中心网站,seo的培训网站哪里好,怎样建设赌博网站,免费营销培训Vue的APP实现下载文件功能,并将文件保存到手机中 文字说明后台核心代码前台核心代码运行截图项目链接 文字说明 本文介绍Vue实现的APP,将文件下载并保存到手机中,为系统提供导出功能;同时支持导入,即选择本地的文件后&…

Vue的APP实现下载文件功能,并将文件保存到手机中

    • 文字说明
    • 后台核心代码
    • 前台核心代码
    • 运行截图
    • 项目链接

文字说明

本文介绍Vue实现的APP,将文件下载并保存到手机中,为系统提供导出功能;同时支持导入,即选择本地的文件后,获取文件内容,并将其上传到服务器中,也可选择对上传的文件内容进行解析,为系统提供导入功能

附带讲解视频,主要实验Vue的5+App的文件上传和下载功能

后台核心代码

后端控制器代码(上传接口)

package com.boot.controller;import com.boot.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.io.InputStream;/*** @author bbyh* @date 2024/6/4 22:58*/
@RestController
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
public class UploadController {@PostMapping("/upload")public Result upload(@RequestBody MultipartFile file) {try {InputStream inputStream = file.getInputStream();byte[] buf = new byte[inputStream.available()];int read = inputStream.read(buf);String fileContent = new String(buf, 0, read);log.info("文件名称:{},文件内容:{}", file.getOriginalFilename(), fileContent);return Result.success("文件上传成功", fileContent);} catch (Exception e) {log.error(e.getMessage(), e);return Result.error("文件上传接口接口服务出错", null);}}
}

下载接口

package com.boot.controller;import com.boot.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;/*** @author bbyh* @date 2024/6/4 22:45*/
@RestController
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
public class DownloadController {@Value("${file.path}")private String filePath;@GetMapping("/download/{fileName}")public void download(@PathVariable String fileName, HttpServletResponse response) {try {response.setContentType("application/octet-stream");response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));try (InputStream inputStream = Files.newInputStream(Paths.get(filePath + fileName))) {byte[] buf = new byte[inputStream.available()];int read = inputStream.read(buf);response.getOutputStream().write(buf, 0, read);}} catch (Exception e) {log.error(e.getMessage(), e);response.reset();Result.writeResult(response, "文件下载接口接口服务出错," + e.getMessage());}}
}

前台核心代码

页面主要代码

<template><div style="display: flex; justify-content: center; align-items: center; height: 100px; width: 100%"><el-button type="primary" @click="selectFile()">选择文件上传</el-button><el-button type="danger" @click="openDownloadDialog()">下载文件</el-button><input type="file" style="display: none" @change="uploadFile($event)" ref="uploadFileRef"></div><el-dialog title="文件下载" width="60%" v-model="data.dialog"><el-input v-model="data.fileName" placeholder="请输入下载文件名"/><template #footer><span class="dialog-footer"><el-button @click="data.dialog = false">取消</el-button><el-button type="primary" @click="downloadFile">确认下载</el-button></span></template></el-dialog>
</template><script setup>
import {downloadFileToLocal, message, postRequest} from "./util";
import {reactive, ref} from "vue";function uploadFile(event) {if (event.target.files.length === 0) {message("warning", "本次未选择文件");return;}const formData = new FormData();const file = event.target.files[0];formData.append("file", file, file.name);postRequest("/upload", formData).then((res) => {if (res.data.code === 200) {message("success", res.data.data);} else if (res.data.code === 500) {message("error", res.data.msg);}});
}const uploadFileRef = ref();async function selectFile() {uploadFileRef.value.click();
}const data = reactive({fileName: undefined,dialog: false,
});function openDownloadDialog() {data.fileName = undefined;data.dialog = true;
}function downloadFile() {downloadFileToLocal("/download/" + data.fileName, data.fileName);
}
</script><style>
* {padding: 0;margin: 0;box-sizing: border-box;
}
</style>

工具类代码

import axios from "axios";
import {ElMessage} from "element-plus";const baseUrl = "http://127.0.0.1:5000";export const downloadFileToLocal = (href, filename) => {const eleLink = document.createElement('a');eleLink.download = filename;eleLink.style.display = 'none';eleLink.href = baseUrl + href;document.body.appendChild(eleLink);eleLink.click();document.body.removeChild(eleLink);
}export function message(type, msg) {ElMessage({message: msg,type: type,center: true,showClose: true,})
}export const postRequest = (url, data) => {return axios({method: 'post',url: baseUrl + url,data: data,})
}

运行截图

页面运行效果
在这里插入图片描述

项目链接

VueApp上传下载文件

http://www.hkea.cn/news/641987/

相关文章:

  • 网站开发方式有哪四种搜索引擎优化课程总结
  • 申请做网站、论坛版主app推广接单
  • 青海网站建设广州seo优化推广
  • 物流公司网站制作模板上海网站关键词排名
  • 广西建设人才网搜索引擎优化的目标
  • 比汉斯设计网站素材图片搜索识图入口
  • php网站架设教程英雄联盟韩国
  • 做毕设好的网站百度客服电话24小时
  • 上海手机网站建设电话咨询seo综合查询系统
  • wordpress 4.6 中文版沈阳seo
  • 文件管理软件天津搜索引擎优化
  • 九亭网站建设全国疫情高峰时间表最新
  • 青岛网站建设公司武汉seo收费
  • mvc网站建设的实验报告怎么做优化
  • 有官网建手机网站千锋教育培训多少钱费用
  • b2c交易模式的网站有哪些百度营销客户端
  • flash 学习网站重庆网站seo多少钱
  • 年终总结ppt模板免费下载网站小红书seo排名规则
  • 自己架设网站口碑营销的产品有哪些
  • 湖北省网站备案最快几天天津百度推广排名优化
  • app在线开发制作平台seo网络优化前景怎么样
  • 商务网站的基本情况网站建设工作总结
  • 山西建设厅网站网络销售怎么聊客户
  • 软装素材网站有哪些seo网络排名优化哪家好
  • 邯郸市做网站建设网络口碑营销案例分析
  • 罗湖网站建设联系电话西安核心关键词排名
  • 如何编写网站电脑清理软件十大排名
  • 怎么给企业制作网站seo关键词排名优化哪好
  • 高仿服装网站建设西安百度关键词推广
  • 网站单页面怎么做的百度seo站长工具