【问题标题】:Create an image input to save to DB and storage folder in Laravel创建图像输入以保存到 Laravel 中的 DB 和存储文件夹
【发布时间】:2018-01-25 11:59:43
【问题描述】:

您好,我有一个 Laravel 博客应用程序,希望在我的帖子下添加图片。我已经阅读了 Laravel Storage 文档,查看了一些教程和讨论,但我仍然很困惑。我正在尝试将图像保存到存储文件夹和数据库的路径以供以后检索。我已经使用php artisan storage:link 链接了我的存储文件。我得到的错误是Call to a member function store() on null

这是我的 HTML 和 ajax 调用:

@section('content')
<div class="container">
  <div class="row">
    <h1>Create your post</h1>
    <div class="form-group">
      <label for="title">Title</label>
      <input type="text" name="title" id="title" class="form-control">
    </div>
    <div class="form-group">
      <label for="post">Post</label>
      <textarea name="post" rows="8" cols="80" id="post" class="form-control"></textarea>
    </div>
    <div class="form-group">
      <label for="image">Add image</label>
      <input type="file" name="image" id="image" class="form-control">
    </div>
    <input type="submit" name="submit" value="Submit Post" id="submit" class="btn btn-primary">
  </div>
</div>

<script>
$(document).ready(function(){
  $("#submit").on("click", function(e){
    e.preventDefault();
    var title = $("#title").val();
    var post = $("#post").val();
    var image = $("#image").val();

    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr("content")
      }
    });

    $.ajax({
      url:'/post/create/create',
      type: "POST",
      data: {
        title: title,
        post: post,
        image: image,
      },

      success:function(response){
        toastr.success(response.response);
      },
      error: function(error){
        toastr.error(error.error)
      }
    });

  });
});


</script>
@endsection

这是我的帖子控制器:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use App\User;
use App\Comment;
use Auth;

class PostController extends Controller{
    public function create(Request $request){
      $this->validate($request, [
        'title' => 'required',
        'post' => 'required',

      ]);

      $path = $request->file('image')->store('images');

      $post = new Post;
      $post->title = $request->input('title');
      $post->post = $request->input('post');
      $post->uploadImage = $path;
      $post->user_id = Auth::user()->id;

      $post->save();

      $response = [
        'response' => 'Post Created Successfully',
        'error' => 'Something went wrong'
      ];

      return response()->json($response);
    }

}

【问题讨论】:

  • 您的问题与存储无关。您的控制器代码绝对没问题。问题是您在 ajax 中发送的图像错误,所以没有要保存的文件
  • 谢谢 - 我将如何在 ajax 中发送它?
  • 你为什么不试试像 plupload 这样的包:github.com/jildertmiedema/laravel-plupload

标签: php laravel


【解决方案1】:

要使用ajax 上传图片,您必须使用FormData,例如:

var formData = new FormData();
var fileData = $('#image').prop('files')[0];
var name     = $('#name').val();
formData.append('fileData', fileData);
formData.append('name', name);

并在 ajax 中使用它,例如:

$.ajax({
    url: 'your url',
    method: 'post',
    data: formData,
    contentType : false,
    processData : false,
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    success: function(response){
        // yoou code here
    }
});

在服务器端,您可以获得formData,并且所有附加值都可以在它们各自的索引上使用。

【讨论】:

  • 我不明白你所有的代码 - 你为什么要声明一个名称变量?它的作用是什么?
  • 普通的ajaxkey : value 不能用于文件上传,formData 用于它,我们可以创建它的一个对象并附加其中的所有元素,就像创建一个隐藏表单并将所有值附加到其中然后提交表单一样。
  • 是的,但是在您的代码中,您有一个名称变量。我在问这个。
  • 这只是一个例子,你可以添加任意数量的值,就像formData.append('key', value);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 2017-07-26
  • 1970-01-01
  • 2019-11-16
相关资源
最近更新 更多