$(function () {
var bar = $('.bar');
var percent = $('.percent');
var showimg = $('# showimg');
var progress = $(".progress");
var files = $(".files");
var btn = $(".btn span");
$("#fileupload").wrap("
");
$("#fileupload").change(function(){ //Select file
$("#myupload").ajaxSubmit({
dataType: 'json', //data The format is json
percentVal = '0%'; //The starting progress is 0%
.html("Uploading..."); //The upload button shows that uploading is in progress. '; //Get progress
bar.width(percentVal); //Upload progress bar width becomes wider
percent.html(percentVal); //Display upload progress percentage
},
success: function(data) { //Success
// Obtain the json data returned from the background, display the file name, size, and delete button data.size+"k)
The picture after
var img = "http://demo.helloweba.com/upload/files/"+data.pic;
showimg.html("
btn.html("Add attachment"); //Upload button restore
},
error:function(xhr){ //Upload failed
btn.html("Upload Failure");
bar.width('0');
files.html(xhr.responseText); //Return failure message
}
});
});
...
});
For more information about the jquery.form plug-in, please refer to the official website of the form plug-in: http://malsup.com/jquery/form/, official website The API and various option settings and examples of the form plug-in are introduced in detail.
Next, the file upload is completed. If the user wants to delete the uploaded file, he can write an ajax post request to complete the deletion operation.
Copy code
The code is as follows:
$(function () {
...continue the above code
$(".delimg").live('click',function(){
var pic = $ (this).attr("rel");
$.post("action.php?act=delimg",{imagename:pic},function(msg){
if(msg==1){
files.html("Delete successfully."); 🎜> alert(msg); 🎜>action.php needs to handle image uploading and deletion. When uploading images, you need to verify the format and size, then upload the images through the move_uploaded_file() method, and finally return data in json format. When deleting a picture, use unlink() to complete the deletion operation.
Copy code
The code is as follows:
$action = $_GET['act'];
if($action =='delimg'){ //Delete image
$filename = $_POST['imagename'];
if(!empty($filename)){ unlink('files/'.$filename );
echo '1'; mypic']['name']; $picsize = $_FILES['mypic']['size']; if ($picname != "") {
if ($picsize > 512000) { //Limit upload size
; if ($ Type! = ".Gif" && $ Type! = ".Jpg") { Echo 'The picture format was wrong! '; exit; }
$rand = rand(100, 999);
$pics = date("YmdHis") . $rand . $type; //Name the picture name
= round($picsize/1024,2); //Convert to kb
$arr = array(
'name'=>$picname,
'pic'=>$pics,
'size'=>$size
);
echo json_encode($arr); //Output json data
}
This article is completed with the help of jquery form plug-in For the single file upload function, there are actually many excellent upload plug-ins currently available, some based on flash and some based on jquery. Typical ones are:
jQuery File Upload
. This plug-in supports multiple file uploads, drag-and-drop uploads, etc. Interested students can learn more about it first.
http://www.bkjia.com/PHPjc/327662.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/327662.html
TechArticle
Many projects require the instant upload function. For example, after selecting a local image, the image will be uploaded and displayed immediately. This article combines examples to explain how to use jQuery and PHP to implement Ajax instant upload...