前后分但文件上传与多文件上传,前端实现
发布日期:2021-06-29 15:52:10 浏览次数:4 分类:技术文章

本文共 1719 字,大约阅读时间需要 5 分钟。

需求

前后分离的开发模式下,实现前端文件上传,包括单文件与多文件

在这里插入图片描述

后端接口

/upload 单文件上传

方法:post
参数:file --文件
响应:

  • 上传成功:
{
status: "ok", url:"http://localhots:8080/file/xxx"}

失败:

{
status: "failed", msg:"
" }

/uploadMul 多文件上传

方法:post
参数:files --文件
响应:

  • 上传成功:
{
status: "ok", upload: 2, succeed:1, detail: [ {
staus: "ok", url:"http://localhots:8080/file/xxx" }, {
staus: "failed", msg:"
" } ]}

失败:

{
status: "failed", msg:"
" }

前端代码

核心代码

  • 单文件
uploadFile:function(){
var formData = new window.FormData() formData.append('file',document.querySelector('input[type=file]').files[0]) var options = {
url:"http://localhost:8080/upload", data: formData, method: 'post', headers:{
'Content-Type':'multipart/form-data' } } //send request axios(options).then( (res)=>{
console.log(res) } ).catch( (error)=>{
console.log(error)} )}
  • 多文件
uploadMulFile:function(){
var formData = new window.FormData() var files= document.querySelector('#mulFile').files; for (let index = 0; index < files.length; index++) {
formData.append('files',files[index]) } var options = {
url:"http://localhost:8080/uploadMul", data: formData, method: 'post', headers:{
'Content-Type':'multipart/form-data' } } //send request axios(options).then( (res)=>{
console.log(res) } ).catch( (error)=>{
console.log(error)} ) }}

完整html文件代码

  • 使用了vue
    
Document

转载地址:https://console.blog.csdn.net/article/details/112061297 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Poj百练 2711:合唱队形 (分类:动态规划)
下一篇:SpringBoot实现一个文件上传服务

发表评论

最新留言

很好
[***.229.124.182]2024年04月07日 22时48分46秒