微笑丿 2019-09-05 11:11 采纳率: 0%
浏览 1242

ant+vue+springboot 上传文件时接收不到文件

 <a-upload
                                name="file"
                                listType="picture-card"
                                :fileList="fileList"
                                :multiple="true"
                                @preview="handlePreview"
                                :beforeUpload="beforeUpload"
                                @change="handleChange"
                        >
                            <div v-if="fileList.length < 1">
                                <a-icon type="plus" />
                                <div class="ant-upload-text">Upload</div>
                            </div>
                        </a-upload>

js代码如下

var formData = new FormData()

                formData.append('file', this.fileList[0]);
                formData.append('userid', '1');
                formData.append('systemid', '886');
                this.axios(
                    {
                        method: 'post',
                        url: 'http://localhost:8086/rest/uploadFileByMulti',
                        processData: false,
                        data: formData
                    })
                    .then((response) => {
                        alert(JSON.stringify(response))
                    })
                    .catch(function (error) {
                        console.log(error)
                    })

后台接受代码如下

 @RequestMapping("/uploadFileByMulti")
    @ResponseBody
    public AjaxResult uploadFileList(@RequestParam("file") 
        MultipartFile file String userid, String systemid) {
......
}

上传时一直报file为空

  • 写回答

2条回答

  • 微笑丿 2019-09-05 14:03
    关注

    找到问题了,vue的filelist是base64格式的 ,需要转为file格式就可以了
    self.imageUrl = file.url || file.thumbUrl
    form.append('image', self.dataURLtoFile(self.imageUrl,file.name))

    dataURLtoFile(dataurl, filename) {//将base64转换为文件
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
    u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {type:mime});
    },

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料