加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 如何通过Axios将文件发送到Laravel

发布时间:2020-12-14 19:38:17 所属栏目:大数据 来源:网络整理
导读:我需要通过Axios将文件从客户端发布到服务器. 这是我的Vuejs代码: methods: { 'successUpload': function (file) { const config = { headers: { 'Content-Type': 'multipart/form-data' } }; axios.post('/Upload/File',file,config).then(function (respo
我需要通过Axios将文件从客户端发布到服务器.

这是我的Vuejs代码:

methods: {
    'successUpload': function (file) {
        const config = { headers: { 'Content-Type': 'multipart/form-data' } };
        axios.post('/Upload/File',file,config).then(function (response) {
            console.log(response.data);
        });
    }
}

这是我用于处理已发送文件的Laravel代码:

public function uploadFile(Request $request)
{
    if($request->hasFile('file'))
      return "It's a File";

    return "No! It's not a File";
}

但它始终返回否它不是文件.

任何帮助将非常感激.

您必须创建一个FormData对象并附加图像文件.
methods: {
  'successUpload': function (file) {

    let data = new FormData();
    data.append('file',document.getElementById('file').files[0]);

    axios.post('/Upload/File',data).then(function (response) {
        console.log(response.data);
    });
  }
}

一个例子是here.

如果有效,请告诉我.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读