簡體   English   中英

Laravel 4圖片上傳

[英]Laravel 4 image upload

嗨,我正在嘗試通過laravel上傳圖像,一切正常,但現在我想將上傳更改為jquery上傳而不是然后我得到500 internal server error

所以當我用Jquery處理事情時它失敗了。 誰知道問題可能是什么? HTML:

{{ Form::open(array('url' => '../public/posts/add', 'class'=>'form-horizontal', 'role' => 'form', 'id' => 'addPin', 'files' => true)) }}

            <div id="validation-errors" class="alert alert-danger" hidden>
                <p>Some errors occured</p>
                <ul></ul>
            </div>

            <!-- Image Type -->
                <span id="type-image" class="type-media">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Title</label>
                        <div class="col-sm-9">
                            {{ Form::text('Image-title', null, array('class' => 'form-control', 'placeholder' => '')) }}
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">Choose file</label>
                        <div class="col-sm-9">
                            {{ Form::file('Image-file') }}
                            <p class="help-block">Only .jpg, .png, .gif, .bmp allowed.</p>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">Description</label>
                        <div class="col-sm-9">
                            {{ Form::textarea('Image-description', null, array('class' => 'form-control', 'rows' => '3')) }}
                        </div>
                    </div>
                </span>


            <div class="modal-footer">
                {{ Form::submit('Close', array('class' => 'btn btn-default', 'data-dismiss' => 'modal')) }}
                {{ Form::submit('Pin it, babe!', array('class' => 'btn btn-info')) }}
            </div>
            {{ Form::close() }}

jQuery的

addPin.on('submit', function() {
    event.preventDefault();
    var errorForm = addPin.find('div#validation-errors');
    $.ajax({
        url: '../public/posts/add',
        type: 'post',
        cache: false,
        data: addPin.serialize(),
        beforeSend: function() {
            errorForm.hide();
            errorForm.find("ul").empty();
        },
        success: function(data) {
            if(data.success == false) {
                var arr = data.errors;
                console.log(arr);
                $.each(arr, function(index, value){
                    if (value.length != 0){
                        errorForm.find("ul").append('<li>'+ value +'</li>');
                    }
                });
                errorForm.show();
                } else {
                location.reload();
            }
        },
        error: function() {
            alert('Something went to wrong.Please Try again later...');
        }
    });
    return false;
} );

PHP

public function postAdd(){
        if (Auth::check()){
                    $rules = array(
                        'Image-title' => 'Required|Min:3|Max:255|alpha_spaces',
                        'Image-description' => 'Required|Min:3',
                        'Image-file' => 'image',
                    );

            $validator = Validator::make(Input::all(), $rules);

            if ($validator->fails()) {
                return \Response::json(['success' => false, 'errors' =>  $validator->getMessageBag()->toArray()]);
            } else {
                        $post = Post::create(array(
                            'user_id'   => Auth::user()->id,
                            'title'     => Input::get('Image-title'),
                            'description' => Input::get('Image-description'),
                            'type'      => 'Image',
                        ));

                        $file = Input::file('Image-file');

                        $destinationPath    = 'img/';
                        $extension          = $file->getClientOriginalExtension();
                        $filename           = 'usr_'.  Auth::user()->id . '_post'.$post->id .'.'. $extension;

                        $file->move($destinationPath, $filename);
                        $post->imgLocation = $filename;
                        $post->save();

                DB::table('board_post')->insert(['board_id'  => 2, 'post_id'   => $post->id]);

                return  \Response::json(['success' => true]);//*/

            }
        }
    }

您的app/storage/logs/laravel.log文件中應該有一個錯誤,指出確切的錯誤。 設置PHP以顯示所有錯誤在開發環境中是首選,因此您可能需要考慮啟用這些錯誤,因此您將獲得比“500 Internal Server Error”更具描述性的消息。

檢查php.ini中的upload_max_filesize

 Echo ini_get ("upload_max_filesize"); 

看到這個

問題是由於Jquery / JS並不真正支持圖片上傳

我修復了問題,當你點擊按鈕,表單得到驗證(通過ajax)並顯示錯誤時,否則它執行正常表單發送(如果我不做event.prefentdefault()

這個現在有用,或許我會再看看它,使它完全成為AJax,但可能不是:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM