如何使用 AJAX、PHP 和 jQuery 上传多个图像
使用 AJAX、PHP 和 jQuery 上传多个图像可能是开发 Web 时一项有用的技能应用程序。让我们来看看一个问题及其解决方案,以帮助您实现这一目标:
问题:
“我在使用 AJAX 上传多个图像时遇到问题。这是我的代码已经写道:“
解决方案:
这里的问题不在于上传多个图像,而是在于通过AJAX发送文件数据。要解决此问题,我们需要修改代码并使用 JSON 对文件进行编码:
更新的 HTML:
<div> <p><strong>更新的 CSS:</strong></p> <pre class="brush:php;toolbar:false">#uploads { display: block; position: relative; } #uploads li { list-style: none; } #drop { width: 90%; height: 100px; padding: 0.5em; float: left; margin: 10px; border: 8px dotted grey; } #drop.hover { border: 8px dotted green; } #drop.err { border: 8px dotted orangered; }
已更新JavaScript:
var display = $('#uploads'); // cache `#uploads`, `this` at `$.ajax()` var droppable = $('#drop')[0]; // cache `#drop` selector $.ajaxSetup({ context: display, contentType: 'application/json', dataType: 'json', beforeSend: function (jqxhr, settings) { // pre-process `file` var file = JSON.parse( decodeURIComponent(settings.data.split(/=/)[1]) ); // add `progress` element for each `file` var progress = $( '<progress></progress>', { class: 'file-' + (!!$('progress').length ? $('progress').length : '0'), min: 0, max: 0, value: 0, 'data-name': file.name, } ); this.append(progress, file.name + '<br>'); jqxhr.name = progress.attr('class'); }, }); var processFiles = function processFiles(event) { event.preventDefault(); // process `input[type=file]`, `droppable` `file` var files = event.target.files || event.dataTransfer.files; var images = $.map(files, function (file, i) { var reader = new FileReader(); var dfd = new $.Deferred(); reader.onload = function (e) { dfd.resolveWith(file, [e.target.result]); }; reader.readAsDataURL(new Blob([file], { type: file.type })); return dfd.then(function (data) { return $.ajax({ type: 'POST', url: '/echo/json/', data: { file: JSON.stringify({ file: data, name: this.name, size: this.size, type: this.type, }), }, xhr: function () { // do `progress` event stuff var uploads = this.context; var progress = this.context.find('progress:last'); var xhrUpload = $.ajaxSettings.xhr(); if (xhrUpload.upload) { xhrUpload.upload.onprogress = function (evt) { progress.attr({ max: evt.total, value: evt.loaded }); }; xhrUpload.upload.onloadend = function (evt) { var progressData = progress.eq(-1); console.log(progressData.data('name') + ' upload complete...'); var img = new Image(); $(img).addClass(progressData.eq(-1).attr('class')); img.onload = function () { if (this.complete) { console.log(progressData.data('name') + ' preview loading...'); } }; uploads.append('<br>
'); }; } return xhrUpload; }, }) .then(function (data, textStatus, jqxhr) { console.log(data); this.find('img[class=' + jqxhr.name + ']') .attr('src', data.file) .before( '' + data.name + '
' ); return data; }, function (jqxhr, textStatus, errorThrown) { console.log(errorThrown); return errorThrown; }); }); }); $.when.apply(display, images).then(function () { var result = $.makeArray(arguments); console.log(result.length, 'uploads complete'); }, function err(jqxhr, textStatus, errorThrown) { console.log(jqxhr, textStatus, errorThrown); }); }; $(document).on('change', 'input[name^=file]', processFiles); // process `droppable` events droppable.ondragover = function () { $(this).addClass('hover'); return false; }; droppable.ondragend = function () { $(this).removeClass('hover'); return false; }; droppable.ondrop = function (e) { $(this).removeClass('hover'); var image = Array.prototype.slice.call(e.dataTransfer.files) .every(function (img, i) { return /^image/.test(img.type); }); e.preventDefault(); // if `file`, file type `image` , process `file` if (!!e.dataTransfer.files.length && image) { $(this).find('.drop-area-label').css('color', 'blue').html(function (i, html) { $(this).delay(3000, 'msg').queue('msg', function () { $(this).css('color', 'initial').html(html); }).dequeue('msg'); return 'File dropped, processing file upload...'; }); processFiles(e); } else { // if dropped `file` _not_ `image` $(this).removeClass('hover').addClass('err').find('.drop-area-label').css('color', 'darkred').html(function (i, html) { $(this).delay(3000, 'msg').queue('msg', function () { $(this).css('color', 'initial').html(html) .parent('#drop').removeClass('err'); }).dequeue('msg'); return 'Please drop image file...'; }); }; };
更新了 PHP:
<?php if (isset($_POST['file'])) { // do php stuff // call `json_encode` on `file` object $file = json_encode($_POST['file']); // return `file` as `json` string echo $file; }
通过实现此修改后的代码,您可以使用 AJAX、PHP 和jQuery。
以上是如何使用 AJAX、PHP 和 jQuery 高效上传多个图像?的详细内容。更多信息请关注PHP中文网其他相关文章!

不同JavaScript引擎在解析和执行JavaScript代码时,效果会有所不同,因为每个引擎的实现原理和优化策略各有差异。1.词法分析:将源码转换为词法单元。2.语法分析:生成抽象语法树。3.优化和编译:通过JIT编译器生成机器码。4.执行:运行机器码。V8引擎通过即时编译和隐藏类优化,SpiderMonkey使用类型推断系统,导致在相同代码上的性能表现不同。

JavaScript在现实世界中的应用包括服务器端编程、移动应用开发和物联网控制:1.通过Node.js实现服务器端编程,适用于高并发请求处理。2.通过ReactNative进行移动应用开发,支持跨平台部署。3.通过Johnny-Five库用于物联网设备控制,适用于硬件交互。

我使用您的日常技术工具构建了功能性的多租户SaaS应用程序(一个Edtech应用程序),您可以做同样的事情。 首先,什么是多租户SaaS应用程序? 多租户SaaS应用程序可让您从唱歌中为多个客户提供服务

本文展示了与许可证确保的后端的前端集成,并使用Next.js构建功能性Edtech SaaS应用程序。 前端获取用户权限以控制UI的可见性并确保API要求遵守角色库

JavaScript是现代Web开发的核心语言,因其多样性和灵活性而广泛应用。1)前端开发:通过DOM操作和现代框架(如React、Vue.js、Angular)构建动态网页和单页面应用。2)服务器端开发:Node.js利用非阻塞I/O模型处理高并发和实时应用。3)移动和桌面应用开发:通过ReactNative和Electron实现跨平台开发,提高开发效率。

JavaScript的最新趋势包括TypeScript的崛起、现代框架和库的流行以及WebAssembly的应用。未来前景涵盖更强大的类型系统、服务器端JavaScript的发展、人工智能和机器学习的扩展以及物联网和边缘计算的潜力。

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。

Python更适合数据科学和机器学习,JavaScript更适合前端和全栈开发。 1.Python以简洁语法和丰富库生态着称,适用于数据分析和Web开发。 2.JavaScript是前端开发核心,Node.js支持服务器端编程,适用于全栈开发。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)