菜单

Php+ajax+h5 to achieve picture upload function

2017年11月23日 - PHPer

This example for everyone to share the realization of Ajax PHP picture upload specific code for your reference, the specific content is as follows

HTML page code

<… DOCTYPE html>

< HTML lang= “en” >

< head>

< meta charset= “UTF-8” >

< title> Title< /title>

< script type= “text/javascript” src= “__PUBLIC__/home/js/jquery-1.11.0.js” > < /script>

< /head>

< body>

< form class=, “form-horizontal”, “role=”, “form”, “id=”, “myForm””

Action= “/index/fileupsend” method= “post””

Enctype= “multipart/form-data” >

Select File: < input type= “file” id= “file1” /> < br />

< input type=, “button”, “Upload”, “value=”, “id=”, “/&gt”;

< span id= “imgWait” > < /span>

< /form>

< script>

$(function () {)

$(“#upload”).Click (function () {)

$(“#imgWait”).Html (“Upload”);

Var formData = new FormData ();

FormData.append (“myfile”, document.getElementById (“file1”).Files[0]);

$.ajax ({)

Url: “/Home/index/fileupsend””,

Type: “POST””,

Data: formData,

/ * *

* false must automatically add the correct Content-Type

* /

ContentType: false,

/ * *

* false must avoid jQuery’s default handling of formdata

* XMLHttpRequest handles the formdata correctly

* /

ProcessData: false,

Success: function (data) {

If (data) {

Alert (upload success)! “);

}

$(“#imgWait”).Html (“upload success”);

},

Error: function () {

Alert (upload failed)! “);

$(“#imgWait”).Hide ();

}

});

});

});

< /script>

< /body>

< /html>

php code

Public function fileupsend () {

$type_pic = $this-> file_upload (‘1’, array (‘jpg’,’gif’,’png’,’jpeg’),’filetest’,’myfile’);

Echo $type_pic[‘img_path’];

}

发表评论

电子邮件地址不会被公开。