微笑丿 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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog