Laravel上传图片

时间:2017-06-07 13:08:20

标签: laravel laravel-5 blade laravel-blade intervention

好的我有一个小问题。

我希望用户能够将图像上传到公共/徽标文件夹,并在div中显示该图像。

但是我在尝试上传时遇到了这两个错误:

enter image description here

JS:

function submitImage() {
    var fd = new FormData($("#upload_form")[0]);
    fd.append('img', $('#img'));
    $.ajax({
        url: 'business',
        data: fd,
        dataType: 'json',
        async: false,
        type: 'post',
        processData: false,
        contentType: false,
        success: function(data) {
            $("#image").attr("src", data.url);
            $('.naviga').append('<div class="alert alert-success">Image Uploaded!<div>');
            $(".alert-success").fadeOut(5000);
        },
        error: function(data) {
            var errors = data.responseJSON; //this will get the errors response data.
            //show them somewhere in the markup
            //e.g
            var errorsHtml = '<div class="alert alert-danger">';
            errorsHtml += errors.img[0]; //showing only the first error.
            errorsHtml += '</div>';
            $('.naviga').append('<div class="alert alert-danger">File type not supported! Use files with image extension only!<div>'); //appending to a <div id="form-errors">
            $(".alert-danger").fadeOut(5000);
        }
    });
}

控制器:

public function image(Requests\ImageRequest $request) {
    if($request->hasFile('img'))
    {
        $image = Input::file('img');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('logo/' . $filename);
        Image::make($image->getRealPath())->resize(200, 200)->save($path);
        $file = $request->file('img');
        return ['url' => url('logo/' . $filename)];
    }
}

刀片:

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

路线::

Route::get('/', function () {
    return view('auth/login');
});
Route::group(['middleware' => ['auth']], function () {
    Route::get('tfgm', 'GuzzleController@tfgm')->name('tfgm');;
    Route::get('odeon', 'GuzzleController@odeon')->name('odeon');;
    Route::get('chronicle', 'GuzzleController@oldham_chronicle')->name('chronicle');;
    Route::get('smokeyard', 'GuzzleController@smokeyard')->name('smokeyard');;
    Route::get('profile/', 'ProfileController@checkid')->name('profile');;
    Route::post('update', 'ProfileController@updateProfile');
    Route::get('create/business', 'BusinessController@addBusiness')->name('createBusiness');
    Route::post('create', 'BusinessController@createBusiness');
    Route::get('business/list', 'BusinessController@viewBusiness')->name('viewBusiness');
    Route::get('business/{name}', 'BusinessController@displayBusiness')->name('displayBusiness');
    Route::post('business/test', 'BusinessController@image');
});
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/redirect/{provider}', 'SocialAuthController@redirect');
Route::get('/callback/{provider}', 'SocialAuthController@callback');

据我所知,我提供了我网站的所有路线

1 个答案:

答案 0 :(得分:0)

我认为在将图像添加到数据时会搞乱。

看看this answer,基本上你可以简化你的jquery