springBoot+vue+element文件上传

1 篇文章 0 订阅

一 前台

  /***图片上传 begin*****/
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            //图片上传成功返回值
            handle_success(res) {
                console.log(res)
                this.addForm.commodityDepict=res.data;
                this.$message.success('图片上传成功')
            },
            /***图片上传 end*****/

 <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form  :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-row>
                    <el-col :span="14">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="商品名称" prop="commodityName">
                                    <el-input v-model="addForm.commodityName" auto-complete="off"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" >
                                <template>
                                    <el-select v-model="commodityType" @change="handleChange">
                                        <el-option v-for="item in selectList" :key="item.whsCode" :label="item.fruitName" :value="item.fruitEnName"></el-option>
                                    </el-select>
                                </template>
                            </el-col>
                        </el-row>

                        <el-form-item label="商品编号" prop="commodityNum">
                            <el-input v-model="addForm.commodityNum" auto-complete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="生产商" prop="manufacturer">
                            <el-input v-model="addForm.manufacturer" auto-complete="off"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="10">
                        <el-upload
                                action="http://192.168.0.105:9091/fileupload.do"
                                list-type="picture-card"
                                :on-preview="handlePictureCardPreview"
                                :on-success="handle_success"
                                :on-remove="handleRemove">
                            <i class="el-icon-plus"></i>
                        </el-upload>
                        <el-dialog :visible.sync="dialogVisible">
                            <img width="100%" :src="dialogImageUrl" alt="">
                        </el-dialog>
                        <el-input v-model="addForm.commodityDepict" style="display: none;" auto-complete="off"></el-input>
                    </el-col>
                </el-row>

                <el-row >
                    <el-col :span="8">
                        <el-form-item label="商品数量">
                            <el-input-number v-model="addForm.commodityAmount" :min="0" :max="20000"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="商品价格">
                            <el-input-number v-model="addForm.commodityPrice" :min="0" :max="20000"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="上架价格">
                            <el-input-number v-model="addForm.webPrice" :min="0" :max="20000"></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
二 springBoot 后台
@PostMapping(value="/fileupload.do")
    @CrossOrigin(allowCredentials ="true")
    public ServerResponse fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        String filename = file.getOriginalFilename();//获得文件原名 如:abc.txt
        // 构建上传文件的存放 "文件夹" 路径
        File fileDir= UploadUtils.getImgDirFile();
        //将要被储存的对象 E:/saving/abc.txt
        File target =null;
        //重命名文件
        int i = filename.lastIndexOf('.');
        String suffix = filename.substring(i, filename.length()); //文件后缀 .txt
        target = new File(fileDir.getAbsolutePath() +'/'+ UUID.randomUUID() + suffix);
        //创建目标文件夹,此时目标文件肯定不存在
        target.getParentFile().mkdir();
        try {
            //储存过程
            file.transferTo(target);
            //返回目标文件的绝对路径
            //System.out.println("文件绝对路径:"+target.getAbsolutePath());
            HttpSession session = request.getSession();
            session.setAttribute("fileUpload",UUID.randomUUID() + suffix);
            return ServerResponse.createBySuccess("fileUploadSucess",UUID.randomUUID() + suffix);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return    ServerResponse.createByErrorMessage("no file upload")
;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现头像上传,可以结合Spring Boot后端框架,Vue前端框架以及Element UI组件库进行实现。 首先,在Vue前端页面中,可以使用Element UI中的Upload组件实现文件上传功能。可以在页面中定义一个Upload组件,设置action属性为上传接口的URL,设置headers属性为请求头部信息,设置on-success属性为上传成功后的回调函数。具体代码如下: ``` <template> <div> <el-upload class="avatar-uploader" action="/api/uploadAvatar" :headers="{ Authorization: 'Bearer ' + token }" :show-file-list="false" :on-success="handleSuccess"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </div> </template> <script> import { getToken } from '@/utils/auth' export default { data() { return { imageUrl: '', token: getToken() } }, methods: { handleSuccess(response) { this.imageUrl = response.data.url } } } </script> ``` 其中,token是用于认证的令牌,可以通过getToken函数获取。handleSuccess函数是上传成功后的回调函数,其中response.data.url表示上传成功后的图片URL。 然后,在Spring Boot后端接口中,可以使用Spring MVC的注解@RequestParam来接收上传的文件。具体代码如下: ``` @RestController @RequestMapping("/api") public class UploadController { @PostMapping("/uploadAvatar") public JsonResult uploadAvatar(@RequestParam("file") MultipartFile file) throws IOException { // 处理上传的文件 return JsonResult.ok("url", "http://www.example.com/avatar.jpg"); } } ``` 其中,@PostMapping注解表示接收POST请求,@RequestParam("file")注解表示接收名为file的文件参数。处理上传的文件后,可以返回一个JsonResult对象,其中包含上传成功后的图片URL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值