Home > Web Front-end > JS Tutorial > body text

jQuery upload image plugin without refresh

PHP中文网
Release: 2017-09-01 16:35:49
Original
2346 people have browsed it

The original jQuery image upload plug-in supports server-side upload, preview, deletion, enlargement, upload quantity and size limits, and callback functions before, during and after uploading.

jQuery upload image plugin without refresh

Usage method
1. First introduce jquery and the css and js of the plug-in. Note that jquery is introduced first

<link href="./css/upload.css" type="text/css" rel="stylesheet" />
<script src="./js/jquery.js"></script>
<script src="./js/upload.js"></script>
Copy after login

2. HTML structure

<div class="upload-box">
    <p class="upload-tip">作品图片:最多可以上传5张图片,马上上传</p>
    <div class="image-box clear">
        <section class="upload-section">
            <div class="upload-btn"></div>
            <input type="file" name="file" id="upload-input" value=""/>
        </section>
    </div>
</div>
Copy after login

3. Plug-in configuration

$("#upload-input").ajaxImageUpload({
    url: &#39;http://www.gouguoyin.cn/demo/store141.html&#39;, //上传的服务器地址
    data: { name:&#39;勾国印&#39; },
    maxNum: 3, //允许上传图片数量
    zoom: true, //允许放大
    allowType: ["gif", "jpeg", "jpg", "bmp",&#39;png&#39;], //允许上传图片的类型
    maxSize :2, //允许上传图片的最大尺寸,单位M
    before: function () {
        alert(&#39;上传前回调函数&#39;);
    },
    success:function(data){
        alert(&#39;上传成功回调函数&#39;);
        console.log(data);
    },
    error:function (e) {
        alert(&#39;上传失败回调函数&#39;);
        console.log(e);
    }
});
Copy after login

4. Server-side processing
There are no special restrictions on server-side processing. As long as the server accepts the data submitted by the file form and processes it, it returns json format data. In the json data The src item must be included, such as {'src':'http://www.gouguoyin.cn/template/default/images/avatar.jpg'}. Here is a simple demonstration using PHP as an example

$file = $_FILES["file"];
if(!isset($file[&#39;tmp_name&#39;]) || !$file[&#39;tmp_name&#39;]) {
    echo json_encode([&#39;code&#39; => 401, &#39;msg&#39; => &#39;没有文件上传&#39;]);
    return false;
}
if($file["error"] > 0) {
    echo json_encode([&#39;code&#39; => 402, &#39;msg&#39; => $file["error"]]);
    return false;
}

$upload_path = $_SERVER[&#39;DOCUMENT_ROOT&#39;]."/upload/";
$file_path   = &#39;http://&#39; . $_SERVER[&#39;HTTP_HOST&#39;]."/upload/";

if(!is_dir($upload_path)){
    echo json_encode([&#39;code&#39; => 403, &#39;msg&#39; => &#39;上传目录不存在&#39;]);
    return false;
}

if(move_uploaded_file($file["tmp_name"], $upload_path.$file[&#39;name&#39;])){
    echo json_encode([&#39;code&#39; => 200, &#39;src&#39; => $file_path.$file[&#39;name&#39;]]);
    return false;
}else{
    echo json_encode([&#39;code&#39; => 404, &#39;msg&#39; => &#39;上传失败&#39;]);
    return false;
}
Copy after login

jQuery upload image plugin without refresh

The above is the detailed content of jQuery upload image plugin without refresh. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template