html 不能上传和存储图像在本地存储和数据库中(Php Laravel)

vdzxcuhz  于 2022-12-16  发布在  PHP
关注(0)|答案(2)|浏览(124)

所以我建立我自己的网站,我有这个功能,其中登录用户可以上传和更改他的头像。这是我第一次这样做,所以我很难让这个工作。我会提供下面的代码,你们可能会看到我不知道的错误。这将是非常感谢,如果你能提供链接,将帮助我改进。提前感谢!

刀片.php文件

<form method='POST' action="{{ route('image-upload')  }}" enctype="multipart/form-data">
   @csrf
     <div class=" overflow-auto" style="padding-top:5%">>
      <div class="p-3">
       <div class="card">
        <div class="card-body" >
          <h4 class="card-title text-info"><strong> Attach your picture </strong></h4>
            <div class="row justify-content-center">
              <div class="col-sm-12 col-lg-4">
               <div class="form-group row">
               <label for="step5_picture" class="col-sm-3 text-right control-label col-form-label">Please upload your photo here:</label>
               <div class="col-sm-9">
               <input class="form-control" type="file" value="" id='upload_picture'  >
             </div>
            </div>
           </div>
          </div>
                
       <a href="/home"  class="btn btn-lg waves-effect waves-light btn-success" id="btn-next"  style="float:right;">Next</a>
      <button class="btn btn-lg waves-effect waves-light btn-info" id="btn-upload"  style="float:right; margin-right:10px;">Upload</button>
                </div>
            </div>
        </div>
    </div>
    </form>

Ajax代码

$("#btn-upload").click(function(e){
    e.preventDefault();
    var data = {
        'photo_filename': $('#upload_picture').val(),
        }

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

  
    $.ajax({
        type: "POST",
        url: "/image-upload",
        data: data,
        dataType: "json",
        success: function (response) { 
        }
        }); 
   });

});

Controller.php文件我的数据库中列的名称也是photo_filename

public function image-upload(Request $request,){
    $data = UserInfoModel::where('app_id', '=' ,Session::get('loginId'))->first();
    $validator=Validator::make($request->all(), [
        'photo_filename' => 'required',
        'photo_filename.*' => 'image|mimes:jpeg,png,jpg,svg|max:5000'
        ]);
        if($request->hasfile('photo_filename')){
            $data->update([
            'photo_filename' => $request->input('photo_filename')
          ]);
            $photo = $request->file('photo_filename')->getClientOrginalName();
            $destination = public_path() . '/public/image';
            $request->file('photo_filename')->move($destination, $photo);
            return back()->with('success', 'Your image has been successfully uploaded!');
        }   
}

Web.php文件

路径::post('/图像上传',[自定义验证控制器::类,'图像上传'])-〉名称('图像上传');

我得到了一个有效负载,它是x1c 0d1x
没有错误,但仍无法上传

tcomlyy6

tcomlyy61#

下面是一个在Laravel中创建文件上传的最小工作示例:
刀片文件:

<form method="POST" enctype="multipart/form-data" action="{{ route('save') }}">
    @csrf
    <input type="file" name="image" placeholder="Choose image" id="image">
    <button>Send</button>
</form>

控制器:

public function save(Request $request) {
    $path = $request->file('image')->store('public/images');
    return $path;
}

web.php:

Route::post('/foobar', [\App\Http\Controllers\FoobarController::class, 'save'])->name('save');

请注意,这里不需要 AJAX 调用,因为html表单将使用CSRF令牌执行POST调用。
还请注意,using hyphens in function names won't work in php.

bvhaajcl

bvhaajcl2#

使用FormData发送表单中的文件,并添加contentType: falseprocessData: false,您可以在https://api.jquery.com/jquery.ajax/中看到contentTypeprocessData的功能设置

var formData = new FormData();
formData.append('photo_filename', $('#upload_picture')[0].files[0])
$.ajax({
  url: "/image-upload",
  type: "post",
  data: formData,
  dataType: 'json',
  contentType: false,
  processData: false,
  success: function(response) {
      
  }
});

相关问题