【问题标题】:Displaying image in a div from controller?从控制器的 div 中显示图像?
【发布时间】:2017-07-26 09:52:46
【问题描述】:

我正在将图像保存到公用文件夹,现在我想在定义的 <div class="logo"> 中显示它如何实现?

控制器:

public function testing(Request $request) {
    if($request->hasFile('img'));
    {
        $image = Input::file('img');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('images/' . $filename);
        Image::make($image->getRealPath())->resize(200, 200)->save($path);
        $file = $request->file('img');
        return response()->file($file);
    }
}

JS:

function submitImage(){
    var fd = new FormData($("#upload_form")[0]);
        fd.append( 'img', $('#img') );

$.ajax({
      url:'template',
      data: fd,
      dataType:'json',
      async:false,
      type:'post',
      processData: false,
      contentType: false,
    });
}

刀片:

<form id="upload_form" action="{{ action('BuilderController@testing') }}" enctype="multipart/form-data" role="form" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="img" id="img" class="form-control filestyle margin images" data-input="false" type="file" data-buttonText="Upload Logo" data-size="sm" data-badge="false" onchange="submitImage();" />
</form>
<div class="logo">
    <img class="images" id="image" src="#" alt="Your Logo"/>
</div>

所以我想把src="#"改成一个保存图片的路径比如"C://xampp/htdocs/laravel/public/image/1.jpg"

【问题讨论】:

  • 为什么不发送带有文件名的 JSON 响应,并使用 public_path() 方法将文件名更改为 #image 的 src跨度>
  • 你能告诉我如何实现的代码吗?所以我可以接受作为答案
  • 你能转储你得到的响应吗?
  • url:'template', 是正确的网址吗?我只是问,因为它似乎与您表单中的操作不同
  • 你所说的不同是什么意思?它应该是正确的,因为它在 localhost/template 上运行

标签: javascript jquery html laravel laravel-blade


【解决方案1】:

将控制器方法中的return 更改为:

return ['url' => url('images/' . $filename)];

然后将success 方法添加到您的ajax 调用中:

$.ajax({
    url:'template',
    data: fd,
    dataType: 'json',
    async: false,
    type: 'post',
    processData: false,
    contentType: false,
    success: function (data) {
        $("#image").attr("src", data.url)
    }
});

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    将控制器代码更改为

    public function testing(Request $request) {
        if($request->hasFile('img'));
        {
            $image = Input::file('img');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $path = public_path('images/' . $filename);
            Image::make($image->getRealPath())->resize(200, 200)->save($path);
            $file = $request->file('img');
            $response = [];
            $response['name'] = $path;
            return json_encode($response);
        }
    }
    

    并在您的 JS 中添加一个成功代码,该代码将更改图像的 url

    $.ajax({
          url:'template',
          data: fd,
          dataType:'json',
          async:false,
          type:'post',
          success: function (data) {
             var img_loc = JSON.parse(data).name;
             $("#image").attr('src', img_loc);
          },
          processData: false,
          contentType: false,
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 1970-01-01
      • 2017-01-10
      • 2019-09-19
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      相关资源
      最近更新 更多