uniapp+springboot 实现单文件上传功能

今天在uniapp+springboot开发时遇到了单文件上传的问题,本文章向大家介绍uniapp+springboot上传单个文件,主要包括uniapp+springboot上传单个文件使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

springboot后端:

Service层:
//修改用户(参数user中必须带参数phone) 注:用户头像修改时前端应有form-data类型,名为image的字段"
public int updateUser(User user, MultipartFile image,HttpServletRequest request) throws IOExc    eption{
        //获取服务器运行地址并加上upload文件路径
        String path = request.getServletContext().getRealPath("/upload/");
        saveFile(image,"D:/SoftWorkSpace/master-goose/src/main/resources/static/"); //测试阶段path写死,方便测试
        user.setHeadImg("/"+image.getOriginalFilename());
        return userMapper.updateUserByPhone(user);
    }
    //文件上传的保存文件
 public void saveFile(MultipartFile img,String path) throws IOException {
        File dir = new File(path);
        if(!dir.exists()){
            dir.mkdir();
        }
        File file = new File(path+img.getOriginalFilename()); //编写代码时path可以写死
        img.transferTo(file);
}

Controller层:
//修改用户(参数user中必须带参数phone,可修改非关键用户信息如:用户名,性别,生日,邮箱和用户头像)
@PostMapping("/user")
@ApiOperation("修改用户(参数user对象中必须带参数phone,可修改非关键用户信息如:" +
            "用户名,性别,生日,邮箱和用户头像) 注:用户头像修改时前端应有form-data类型,名为image的字段")
public int updateUser(User user, MultipartFile image, HttpServletRequest request) throws IOException {
      return userService.updateUser(user,image,request);
}

uniapp前端:

View:
<template>
    <view class="container">
        <view>上传文件</view>
        <button type="primary" @click="uploadFile">点击上传文件</button>
    </view>
</template>

JS:
<script>
    export default {
        data() {
            return {
                fileName:"xxx.jpg"
            }
        },
        
        methods: {
            uploadFile(){
                var _this=this;
                uni.chooseImage({
                    success(res) {
                        console.log(res)
                        var tempFilePath=res.tempFilePaths;
                        uni.uploadFile({
                            url:'http://localhost:8080/user', //自己的后端接口(默认发送post请求)
                            filePath:tempFilePath[0],
                            name:"image",  //这里应为自己后端文件形参的名字
                            formData:{
                                "fileName":_this.fileName //其他属性
                            },
                            
                            success(res) {
                                console.log(res.data)
                            }
                        })
                    }
                })
            }
        }
    }
</script>

总结:

uniapp的uni.request请求是以字节流的方式请求后端,无法实现文件上传功能,因此应该使用uniapp内置的API -- uni.uploadFile实现文件上传其中方法中其他的form-data也可以在上传中传递给后端,uni.uploadFile提交的表单属性自动将本地资源上传到开发者服务器,客户端发起一个 POST 请求,其中 content-type 为 multipart/form-data。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
uniapp+springboot实现登录的步骤如下: 1. 首先,你需要创建一个Spring Boot项目。可以参考引用\[1\]和引用\[2\]中提到的方法来创建一个新的Spring Boot项目。 2. 在后端项目中配置数据库,并创建相应的表。你可以使用MySQL或其他数据库来存储用户信息。可以参考引用\[1\]中提到的方法来配置数据库和创建表。 3. 在后端项目中编写相应的类和接口来处理用户登录的逻辑。你可以创建一个UserController类来处理用户登录请求,并编写相应的接口方法。可以参考引用\[1\]中提到的方法来编写类和接口。 4. 在后端项目中编写相应的XML文件来封装返回信息。你可以使用MyBatis或其他ORM框架来操作数据库,并将查询结果封装成JSON格式返回给前端。可以参考引用\[1\]中提到的方法来编写XML文件。 5. 在前端项目中下载uni-app插件,并进行相应的配置。你可以使用uni-app来开发跨平台应用,并编写前端页面和逻辑。可以参考引用\[3\]中提到的方法来下载插件和进行配置。 6. 在前端项目中编写登录页面和相关逻辑。你可以使用Vue来编写前端页面,并使用uni-app提供的组件和API来实现登录功能。可以参考引用\[3\]中提到的方法来编写登录页面和相关逻辑。 通过以上步骤,你可以实现uniapp+springboot的登录功能。前端页面通过uni-app编写,后端逻辑通过Spring Boot实现,前后端通过接口进行数据交互,实现用户登录功能。 #### 引用[.reference_title] - *1* *2* [springboot+uniapp实现注册登录](https://blog.csdn.net/weixin_46014186/article/details/126066903)[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^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [uniapp+Springboot实现登录](https://blog.csdn.net/m0_66403070/article/details/130596594)[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^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xxxy _

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值