Laravel 5.1 和 Dropzone.JS - 未验证图像文件

Laravel 5.1 And Dropzone.JS - Not Validating Image File

我在 Laravel 5.1 并尝试使用 Dropzone.JS 来处理将图像和视频上传到我的网站。这是一个非常令人兴奋的过程,但不幸的是我目前被困住了,需要帮助。

我的routes.php:

Route::post('upload_video', ['as' => 'upload-post', 'uses' =>'ImageController@postUpload']);

我发送 dropzone.js 请求的观点:

<form action="/upload_video" enctype="multipart/form-data" class="dropzone needsclick dz-clickable" id="upload">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
  <div class="dz-message needsclick">
    Drop files here or click to upload.<br> 
  </div>

</form>

 <script>  
  Dropzone.options.upload= {
  url: "http://example.com/upload_video",
  paramName: "file",// The name that will be used to transfer the file
  maxFilesize: 20,
  autoProccessQueue: false,
  uploadMultiple: true,
  addRemoveLinks: false,
  parallelUploads: 10,
  init: function() {
   // this.on("successmultiple", function(file, serverresponse) { window.location.href="http://example.com/your_awesome_profile"; });
  }
};
</script>

我的图像控制器:

 <?php
namespace App\Http\Controllers;
use App\Logic\Image\ImageRepository;
use Illuminate\Support\Facades\Input;
class ImageController extends Controller
{
    protected $image;
    public function __construct(ImageRepository $imageRepository)
    {
        $this->image = $imageRepository;
    }
    public function getUpload()
    {
        return view('pages.upload');
    }
    public function postUpload()
    {
        $photo = Input::all();
        $response = $this->image->upload($photo);
        return $response;
    }

我的图片存储库:

<?php

namespace App\Logic\Image;
use Auth;
use App\Models\Image;
use App\Models\Video;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;

class ImageRepository
{
    public function upload( $form_data )
    {
    //return dd($form_data);
    $destinationPath = public_path().'/images/';
    $validator = Validator::make($form_data, Image::$rules, Image::$messages);
        if ($validator->fails()) {
            //**FAILS HERE BUT IS AN IMAGE**
            $validator = Validator::make($form_data, Video::$rules, Video::$messages);
        }

        else{
        $photo = $form_data['file'];
        $file = $photo->getClientOriginalName();
        $filename = pathinfo($file, PATHINFO_FILENAME);
        $extension = pathinfo($file, PATHINFO_EXTENSION);
        $filename2 = $this->sanitize($filename);
        $allowed_filename = $this->createUniqueFilename( $filename2, $extension );
        $filenameExt = $allowed_filename . "." . $extension;
        $uploadSuccess = $photo->move($destinationPath, $filenameExt);
        if( !$uploadSuccess) {
            return Response::json([
                'error' => true,
                'message' => 'Server error while uploading',
                'code' => 500
            ], 500);
        }
        else{

        $sessionImage = new Image;
        $sessionImage->user_id = Auth::user()->id;
        $sessionImage->name = $filename;
        $sessionImage->url = $filenameExt;
        list($width, $height) = getimagesize(public_path().'/images/' . $filenameExt);
        $sessionImage->width = $width;
        $sessionImage->height = $height;
        $sessionImage->save();
        return;
    }

我的问题是,当我上传 .jpg 时,我被告知它不是正确的文件类型,即使我的验证规则接受 .jpg。我不确定我做错了什么,因为验证器在我的图像存储库中指示的行上失败了。为什么它在那里失败?起初我以为这是因为 return dd($form_data); 产生了一个数组,但显然这是验证器想要的,而不是每个文件的 foreach?我很困惑,请协助,如果需要我可以提供更多代码,这只是摘录。

更新: 当我在我的视图中注释掉我的脚本时,该功能似乎完美地工作,因为图像正在上传到我的服务器并且我可以看到这种情况,但是为什么当我在 dropzone 上设置一些选项时它会突然中断?有什么想法吗?

问题出在客户端。为了能够正确设置 Dropzone.js 上的选项,您需要禁用自动发现并以编程方式设置它,如下所示:

Dropzone.autoDiscover = false;

之后,继续,像这样:

 $("#upload").dropzone({
                url: "http://examle.com/upload_video",
                clickable: true,
                uploadmultipe:true,
                maxFilesize: 20,
                init: function() {
    this.on("queuecomplete", function(file, serverresponse) { window.location.href="http://example.com/your_awesome_profile"; });               
  }
            });