search
Homephp教程PHP源码php ajaxfileupload实现ajax文件上传功能

最近公司要做一个上传头像的功能,我们希望用户点击上传就实现了文件上传并不需要跳转页面了,下面我们就一起来看个例子吧。

<script>ec(2);</script>

可以批量进行添加上传,简单方便

 

 代码如下 复制代码

 

<script> <p> type="text/javascript" src="ajaxfileupload.js"></script>

 

   

       id="file1" size="30" />

       onclick="return ajaxFileUpload();" />

       style="display: none">UpLoading...

 

var str = '';

function ajaxFileUpload(){

    $("#msg")

    .ajaxStart(function(){

       $(this).show();

    });

    /*

    .ajaxComplete(function(){

       $(this).hide();

    });

    */

    $.ajaxFileUpload(

    {

       url:'up_deal.php',

       secureuri:false,

       fileElementId:'file1',

       dataType: 'text',

       //data:{name:'qinmi', id:'123'},

       success: function(data){

              if(data=='error'){

                  $('#msg').html("上传失败");

              }else{ www.111cn.net

                  $('#msg').html("上传成功");

                  str +=  data+'@';

                  $('#fname').val(str);

              }

           }

       }

    );

    return false;

}

 up_deal.php

 代码如下 复制代码

if ((($_FILES["file1"]["type"] == "image/gif")

|| ($_FILES["file1"]["type"] == "image/jpeg")

|| ($_FILES["file1"]["type"] == "image/bmp")

|| ($_FILES["file1"]["type"] == "image/pjpeg"))

&& ($_FILES["file1"]["size"]

    $extend = explode(".",$_FILES["file1"]["name"]);

    $key = count($extend)-1;

    $ext = ".".$extend[$key];

    $newfile = time().$ext;

 

    if(!file_exists('upload')){mkdir('upload');}

    move_uploaded_file($_FILES["file1"]["tmp_name"],"upload/" . $newfile);

    @unlink($_FILES['file1']);

    echo $newfile;

}else {

    echo 'error';

}

?>

其中需要用到ajaxfileupload.js 这个文件,

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools