PHP and ajax implement asynchronous uploading of files or image code sharing

小云云
Release: 2023-03-19 22:08:02
Original
1243 people have browsed it

本文主要和大家详细介绍php+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()

相关推荐:

Ajax表单异步上传文件实例代码详解

利用jQuery异步上传文件的插件用法分享

Html完成异步上传文件的Jquery代码实例


The above is the detailed content of PHP and ajax implement asynchronous uploading of files or image code sharing. 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!