Detailed explanation of how PHP uses ajax to implement asynchronous uploading of files (pictures)

黄舟
Release: 2023-03-14 10:36:01
Original
1886 people have browsed it

这篇文章主要为大家详细介绍了php+ajax实现异步上传文件或图片功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家分享了ajax异步上传文件或图片功能的具体代码,供大家参考,具体内容如下


//html代码 


Copy after login


//js代码 (':button').click(function(event) {   //formdata储存异步上传数据 var formData = new FormData($('form')[0]); formData.append('file',$(':file')[0].files[0]); //坑点: 无论怎么传数据,console.log(formData)都会显示为空,但其实值是存在的,f12查看Net tab可以看到数据被上传了 $.ajax({ url:'formtest.php', type: 'POST', data: formData, //这两个设置项必填 contentType: false, processData: false, success:function(data){ console.log(data) var srcPath = data; console.log();      //注意这里的路径要根据自己的储存文件的路径设置 $('.picDis img').attr('src', '..'+srcPath); } }) });
Copy after login

php:


Copy after login

点击上传图片并发送后, 可以看到页面上显示出图片, 查看本地文件夹可以看到文件也已储存到服务器.在客户端实现异步上传的关键在于FormData,关于这部分这里有详细介绍: FormData()

The above is the detailed content of Detailed explanation of how PHP uses ajax to implement asynchronous uploading of files (pictures). 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!