Home PHP Framework ThinkPHP Detailed explanation of how thinkPHP uses ajax to asynchronously upload images and display and delete them

Detailed explanation of how thinkPHP uses ajax to asynchronously upload images and display and delete them

Mar 22, 2021 pm 05:24 PM
ajax php thinkphp5

The following tutorial column will introduce to you how thinkPHP uses ajax to asynchronously upload images and display and delete them. I hope it will be helpful to friends in need! thinkPHP uses ajax to asynchronously upload pictures and display and delete them

In the process of learning tp5 recently, there is a posting function in the project to select theme pictures. As follows:

# Using the original file upload processing, although the uploaded image can be displayed in real time through the original js statement, this will involve many compatibility issues. Use ajax technology to realize the function of selectively deleting selected pictures without compatibility issues.

Detailed explanation of how thinkPHP uses ajax to asynchronously upload images and display and delete them

Form file form:

Copy after login
    主题图片:                   图片上传               
If we need to send an Ajax request, of course the form cannot meet our needs. Therefore, we need to associate a click event with the form to help us Make an Ajax request and select an image.
When we click the upload image button, the image selection is triggered to implement Ajax upload.

JavaScript code:

<script></script>
<script>
    function upimg(obj)
    {
        if( obj.value == "" ) {
            return;
        }
        var formdata = new FormData();
        //<input type="file" name="img" value="" />
        formdata.append("img" , $(obj)[0].files[0]);//获取文件法二
        $.ajax({
            type : &#39;post&#39;,
            url : &#39;/home/note/upimg&#39;, //接口
            data : formdata,
            cache : false,
            processData : false, // 不处理发送的数据,因为data值是Formdata对象,不需要对数据做处理
            contentType : false, // 不设置Content-type请求头
            success : function(response){
                console.log(response);
                var html = &#39;<p style="position: relative;margin-right: 20px;margin-bottom: 15px;width: 132px;display: inline-block;border: 1px solid #CCC;background:#EEE;">&#39;
                        +&#39;<span style="display: block;width: 120px;height: 80px;border: 1px solid #F2F1F0;margin: 5px;overflow: hidden;">&#39;
                        +&#39;<img  src="/static/imghw/default1.png"  data-src="&#39;+response+&#39;"  class="lazy"     style="max-width:90%" / alt="Detailed explanation of how thinkPHP uses ajax to asynchronously upload images and display and delete them" >&#39;
                        +&#39;&#39;
                        +&#39;<input type="hidden" name="imgs[]" value="&#39;+response+&#39;" />&#39;
                        +&#39;<a onclick="delImg(this);" style="z-index: 10;display: block;top: -8px;cursor:pointer;right: -8px;position:absolute;width: 20px;height: 20px;background: #CCC;border-radius:100%;text-align:center;line-height: 20px;border: 1px solid #C1C1C1;color: #555;">X&#39;
                        +&#39;&#39;;

                $(&#39;#img-list-box&#39;).append(html);
            },
            error : function(){ }
        });
    }

    function delImg(obj)
    {
        $(obj).parent(&#39;p&#39;).remove();
    }
</script>
Copy after login

After clicking to select the image, it is handed over to the server for processing. php interface file:

    public function upimg()
    {
        //验证
        $file = request()->file('img');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                $img_src = '/uploads/'.$info->getSaveName();
                echo $img_src; //返回ajax请求
            }else{
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }
    }
Copy after login
Improved renderings:


The above is the detailed content of Detailed explanation of how thinkPHP uses ajax to asynchronously upload images and display and delete them. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Services CakePHP Services Sep 10, 2024 pm 05:26 PM

This chapter deals with the information about the authentication process available in CakePHP.

See all articles