繁体   English   中英

使用Laravel进行blueimp / jQuery-File-Upload如何集成?

[英]blueimp/jQuery-File-Upload with Laravel How to integrate?

试图构建我的上传图像部分我的网站,并希望使用blueimp / jQuery-File-Upload而不是从头开始硬编码。 但是我也是新手,你能告诉我如何将该插件与我的Laravel结构集成吗?

我在哪里放置所有文件? 在供应商文件夹? 或者我应该拆分所有文件夹并将他们的js文件夹放在我的等等???

如果你知道一个教程它甚至更好......用谷歌找不到任何好的东西。

谢谢

您可以尝试使用此代码发布以帮助其他人。

第一步是定义上传页面和上传处理Route ,如下所示:

Route::get('image_', function() {
    return View::make('image.upload-form');
});

Route::post('image_updade', 'ImageController@postUpload');

使你的image.upload-form视图像这样(我使用的是简单的HTML,而不是Blade模板):

<?php echo Form::open(array('url' => 'image_updade', 'files' => true, 'id' => 'myForm')) ?> 
    Name: <input type='file' name='image' id='myFile'/>
    <br/>
    Comment: <textarea name='comment'></textarea>
    <br/>
    <input type='submit' value='Submit Comment' /> 
<?php echo Form::close() ?>

现在您需要在该视图页面的<HEAD>标记中添加JavaScript文件:

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js'></script> 
<script src='http://malsup.github.com/jquery.form.js'></script> 

<script> 
    // Wait for the DOM to be loaded 
    $(document).ready(function() {

        // Bind 'myForm' and provide a simple callback function 
        $('#myForm').ajaxForm(function() {
            alert('Thank you for your comment!');
        });

        $('#myFile').change(function() {
            $('#myForm').submit();
        });
    });
</script> 

最后,这是一个ImageController@postUpload控制器获取上传文件的代码的简单示例,并将其移动到目标文件夹:

<?php

    class ImageController extends BaseController {

        public function getUploadForm() {
            return View::make('image/upload-form');
        }

        public function postUpload() {

            $file = Input::file('image');
            $input = array('image' => $file);
            $rules = array( 'image' => 'image');
            $validator = Validator::make($input, $rules);

            if ( $validator->fails() ){
                return Response::json(['success' => false, 'errors' => $validator->getMessageBag()->toArray()]);
            }
            else {
                $destinationPath = 'files/';
                $filename = $file->getClientOriginalName();
                Input::file('image')->move($destinationPath, $filename);
                return Response::json(['success' => true, 'file' => asset($destinationPath.$filename)]);
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM