Use uploadify to upload pictures in tp5

1. First, you need to install uploadify, the installation location is as shown in the figure:

2. Introduce css, js code into the page

<link rel="stylesheet" type="text/css" href="__STATIC__/admin/uploadify/uploadify.css" />
{load href="__STATIC__/admin/uploadify/jquery.uploadify.min.js" /}
{load href="__STATIC__/admin/js/image.js" /}

image.js:

$(function(){
    $("#file_upload").uploadify({
        swf  :  swf,
        uploader  :  image_upload_url,
        buttonText  : '图片上传',
        fileTypeDesc: 'Image files',
        fileObjName: 'file',
        fileTypeExts: '*.gif;*.jpg;*.png',
        onUploadSuccess: function(file, data, response){
            if(response){
                var obj = JSON.parse(data);
                $('#upload_org_code_img').attr("src", obj.data);
                $('#file_upload_image').attr("value", obj.data);
                $('#upload_org_code_img').show();
            }
        }
    });
});

3. There needs to be a container in the page

<input id="file_upload"  type="file" multiple="true" >
<img style="display: none" id="upload_org_code_img" src="" width="150" height="150">
<input id="file_upload_image" name="image" type="hidden" multiple="true" value="">


input is used to upload the image
img is used to display the thumbnail after the upload is successful

4. Define swf image_upload_url in inage.js

<script>
    swf = '__STATIC__/admin/uploadify/uploadify.swf';
    image_upload_url = "{:url('image/upload')}";
</script>

5. Controller

The upload method under the image controller

public function upload(){
        $file = Request::instance()->file('file');
        //把图片上传到指定的文件夹中
        $info = $file->move('upload');

        if($info && $info->getPathname()){
            $data = [
                'status' => 1,
                'message' => 'OK',
                'data' => '/'.$info->getPathname(),
            ];
            echo json_encode($data);exit;
        }

        echo json_encode(['status' => 0, 'message' => '上传失败']);
    }

Tip: When using uploadify, you need to enable flash in your browser

Guess you like

Origin blog.csdn.net/qq_43737121/article/details/105601962