vue如何实现文件的上传下载功能?
Admin 2021-10-29 群英技术资讯 2144 次浏览
这篇文章给大家分享的是vue如何实现文件的上传下载功能,这里会使用到的技术除了vue,还有axios、springboot。实现思路及代码如下,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。
一、文件上传(基于axios的简单上传)
实现思路:通过h5 :input元素标签进行选择文件,获取所选选择的文件路径,new fromdata对象,设置fromdata的参数,设置axios对应的请求头,最后通过axios发送post请求后端服务。后端服务同过MultipartFile进行文件接收。具体代码如下:
前端代码:
1、创建vue对象
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import http from 'axios'
Vue.config.productionTip = false;
Vue.prototype.$http=http;
window.vm=new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
2、实现上传组件
在input标签中添加改变事件监听,当发生改变时调用up方法。
<template>
<div class="hello">
<input
class="file"
name="file"
type="file"
accept="image/png, image/gif, image/jpeg"
@change="up"
/>
</div>
</template>
<script>
export default {
name: "HelloWorld",
props: {
msg: String
},
methods: {
up(e) {
let file = e.target.files[0];
alert(file.name);
console.log(file);
let param = new FormData(); //创建form对象
param.append("file", file); //通过append向form对象添加数据
console.log(param.get("file")); //FormData私有类对象,访问不到,可以通过get判断值是否传进去
let config = {
headers: { "Content-Type": "multipart/form-data" }
}; //添加请求头
this.$http
.post("http://127.0.0.1:8081/data/up", param, config)
.then(response => {
console.log(response.data);
}).catch(
error=>{
alert("失败");
}
);
}
}
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
</style>
后端代码:
上传文件代码
@RequestMapping(value = "/up", method = RequestMethod.POST)
@ResponseBody
public Result<String> uploade(@RequestParam("file") MultipartFile file) {
try {
log.error("开始上传!!!");
String originalFilename = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
String path="d:/2020test/";
File file1 = new File(path + originalFilename);
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();
}
file.transferTo(file1);
log.info("上传成功!");
} catch (IOException e) {
e.printStackTrace();
}
Result<String> stringResult = new Result<String>();
stringResult.setMsg("sue");
stringResult.setData("file");
return stringResult;
}
二、文件下载
通过response输出流返回文件内容,核心代码设置下载文件的名字(res.setHeader(“Content-Disposition”, “attachment;filename=” + java.net.URLEncoder.encode(realFileName.trim(), “UTF-8”));)
@RequestMapping(value = "/get", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse res) {
String realFileName="C:/Users/xiongyi/Desktop/12.xls";
File excelFile = new File(realFileName);
res.setCharacterEncoding("UTF-8");
res.setHeader("content-type", "application/octet-stream;charset=UTF-8");
res.setContentType("application/octet-stream;charset=UTF-8");
//加上设置大小下载下来的.xlsx文件打开时才不会报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
// res.addHeader("Content-Length", String.valueOf(excelFile.length()));
try {
res.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(realFileName.trim(), "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(new File(realFileName)));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
}
}
}
Result<String> stringResult = new Result<String>();
stringResult.setMsg("sue");
stringResult.setData("nimabi");
}
以上就是关于vue实现文件上传下载功能的介绍啦,本文只是提供了一种实现思路,代码仅供参考,需要的朋友可以了解看看,希望对大家学习vue框架有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
响应式基本原理是基于Object.defineProperty(obj, prop, descriptor), descriptor里面可以定义get和set方法,可以在获取属性值事触发get方法(可以收集依赖),设置属性值时触发set方法(更新依赖)。
这篇文章主要介绍了vue导入.md文件的步骤(markdown转HTML),帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下
这篇文章主要为大家介绍了React 和 Redux的关系,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
vue如何实现筛选功能?大家在一些网站或者软件上,应该都见过筛选功能,例如先筛选出符合条件的信息再搜索,这样能够提高搜索的准确度,因此这篇就主要仿写teambition软件的筛选功能,给大家介绍一下筛选功能是如何实现的。
setTimeout函数设定页面延时3秒刷新或跳转,这个使用也很广泛,比如我们添加完一条数据之后,希望先弹出添加成功的标志之后再刷新列表,这个时候可能就需要使用setTimeout页面延时刷新功能,使用很简单,代码如下: //假如有个提示框layer.msg(用户添加成功
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008