我在Laravel 5.1中,並試圖使用Dropzone.JS處理上傳圖像和視頻到我的網站。這是一個非常令人興奮的過程,但不幸的是我目前陷入困境,需要幫助。Laravel 5.1和Dropzone.JS - 未驗證圖像文件
我routes.php
:
Route::post('upload_video', ['as' => 'upload-post', 'uses' =>'[email protected]']);
我的觀點從我發送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);
yeilds數組,但顯然這是驗證者想要的,而不是每個文件的foreach
?我很困惑,請協助,如果需要,我可以提供更多的代碼,這只是摘錄。
更新:當我註釋掉我的腳本在我看來,該功能似乎在圖像完美地工作被上傳到我的服務器,我可以看到這種情況出現,但爲什麼當我設置了懸浮窗的一些選項它突然中斷嗎?有任何想法嗎?