【问题标题】:Call to a member function getClientOriginalExtension() on string调用字符串上的成员函数 getClientOriginalExtension()
【发布时间】:2017-05-12 06:14:01
【问题描述】:

在我的 laravel 控制器中,当我尝试上传图片时出现此错误..

我知道问题出在哪里 这是在我的控制器里面

  $image = $request['images'];
        if ($image)
        {
            $imgfile = $request['images'];                                                                                                                                                                 
            $imgpath = 'uploads/users/images/'.$user->id;
            File::makeDirectory($imgpath, $mode = 0777, true, true);
            $imgDestinationPath = $imgpath.'/'; // upload folder in public directory
            $ext1 = $imgfile->getClientOriginalExtension();
            $filename1 = uniqid('vid_').'.'.$ext1;
            $usuccess = $imgfile->move($imgDestinationPath, $filename1);

        }

这是我的 jquery 代码,我的表单 ID 是更新配置文件,我通过其名称获取所有字段值

$("#updateprofile").on('submit', function(e){
        e.preventDefault(e);
        var redirect_url = $(this).find("[name='redirect_url']").val();
        var url = $(this).attr('action');
        var method = $(this).attr('method');
           var myData = {
                name: $(this).find("[name='name']").val(),
                gender:$(this).find("[name='gender']").val(),
                lastname:$(this).find("[name='lastname']").val(),
                email:$(this).find("[name='email']").val(),
                mobile:$(this).find("[name='mobile']").val(),
                address:$(this).find("[name='address']").val(),
                city: $(this).find("[name='city']").val(),
                state:$(this).find("[name='state']").val(),
                country:$(this).find("[name='country']").val(),
                pin:$(this).find("[name='pin']").val(),
                images: document.getElementById('imageToUpload').files[0],
            }
        console.log("data", myData);
       $.ajax({
              type: "PUT",
              url: url,
              dataType: 'JSON',
              data: myData,
              success: function(data) {
                  console.log(data);
                  //alert("Profile update successfully")
                  //window.location.href = redirect_url;
              },
              error: function(jqXHR, textStatus, errorThrown) {
                  alert("das");
                  console.log(JSON.stringify(jqXHR));
                  console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
              }
          });
    });

【问题讨论】:

  • $imgfile 是一个字符串,所以$request['images'] 可能只给你文件名,而不是文件对象本身。根据documentation,您可以通过$request->file('image') 访问您的文件。还是有多个文件?
  • 发布 print_r($request['images']);\
  • 是的,但是在给 $request->file('image') 时,它会在我的控制台窗口 jquery.min.js:3130 Uncaught TypeError: Illegal invocation 中向我显示这样的错误
  • Jquery 不会在后端给你一个错误,你似乎向浏览器发送了一些不正确的东西。
  • @TobiasF。我听不懂你在说什么

标签: php jquery ajax image laravel-5.4


【解决方案1】:

试试这个(假设您的表单输入的idname 的值为images),

 if($request->hasFile('images')) {
            $imgfile = Input::file('images');
            $imgpath = 'uploads/users/images/'.$user->id;
            File::makeDirectory($imgpath, $mode = 0777, true, true);
            $imgDestinationPath = $imgpath.'/';
            $ext1 = $imgfile->getClientOriginalExtension();
            $filename1 = uniqid('vid_').'.'.$ext1;
            $usuccess = $imgfile->move($imgDestinationPath, $filename1);
        }

在您的 jQuery 代码中: 尝试改变

images: document.getElementById('imageToUpload').files[0],

images: document.getElementById('imageToUpload').files[0].value,

当您通过表单上传文件时,您应该在 ajax 调用中指定 contentType: 'multipart/form-data',

【讨论】:

  • 感谢 Sapnesh,但它显示了这样的错误 Uncaught TypeError: Illegal invocation
  • @Karthiga 除了 jQuery 错误之外,还可以按预期存储图像吗?
猜你喜欢
  • 2020-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多