


How to implement file upload with Ajax and form+iframe (detailed explanation with pictures and text)
This time I will bring you the method of file upload with Ajax and form+iframe (detailed explanation with pictures and texts). What are the precautions for Ajax and form+iframe to implement file upload? , the following is a practical case, let’s take a look.
Since the advent of html5, file uploading has become very simple. It is very convenient to solve the file upload function that needs to be used in the project. HTML5 supports multiple image uploads, ajax uploads, previews of images before uploading, and drag-and-drop image uploading. It is purely implemented using the file control and has very little JS code. It is hard not to praise it!HTML5Ajax upload
The upload implementation of html5 requires file control and XMLHttpRequest request. The following is an upload plug-in I encapsulated:function fileUpload(options) { var opts = options || {}; var func = function() {}; this.fileInput = opts.fileInput || null; this.url = opts.url || ''; this.fileList = []; this.onFilter = opts.onFilter || function(f) {return f;}; //选择文件组的过滤方法 this.onSelect = opts.onSelect || func; //文件选择后 this.onProgress = opts.onProgress || func; //文件上传进度 this.onSuccess = opts.onSuccess || func; //文件上传成功时 this.onFailure = opts.onFailure || func; //文件上传失败时; this.onComplete = opts.onComplete || func; //文件全部上传完毕时 this.init(); } fileUpload.prototype = { dealFiles: function(e) { //获取要上传的文件数组(用户选择文件后执行) var files = e.target.files || e.dataTransfer.files; this.fileList = this.onFilter(files); for(var i = 0, file; file = this.fileList[i]; i++){ //增加唯一索引值 file.index = i; } this.onSelect(this.fileList); return this; }, removeFile: function(fileDelete) { //删除某一个文件 var arrFile = []; for(var i = 0, file; file = this.fileList[i]; i++){ if (file != fileDelete) { arrFile.push(file); } } this.fileList = arrFile; return this; }, removeAll: function() { //清空文件队列 this.fileList = []; return this; }, uploadFile: function() { //上传文件 var me = this; for(var i = 0, file; file = this.fileList[i]; i++){ (function(file) { var formData = new FormData(); var xhr = new XMLHttpRequest(); if (xhr.upload) { xhr.upload.addEventListener("progress", function(e) { // 上传中 me.onProgress(file, e.loaded, e.total); }, false); xhr.onreadystatechange = function(e) { // 文件上传成功或是失败 if (xhr.readyState == 4) { if (xhr.status == 200) { me.onSuccess(file, xhr.responseText); me.removeFile(file); if (!me.fileList.length) { me.onComplete(); //上传全部完毕。执行回调 } } else { me.onFailure(file, xhr.responseText); } } }; // 开始上传 formData.append('file', file); xhr.open("POST", me.url, true); xhr.send(formData); } })(file); } }, init: function() { var me = this; //文件选择控件选择 if (me.fileInput) { me.fileInput.addEventListener("change", function(e) { me.dealFiles(e); }, false); } } };
form form submitted to iframe
html code:
<iframe name="demoIframe" style="display:none"></iframe> <form target="demoIframe" action="upload.php" method="post" enctype="multipart/form-data"> <input class="filename" type="file" name="fileLabel"> <input type="submit" value="提交"> </form>
{ "code": "200", "success": true, "data": { ... } }
$('iframe').on('load', function() { var responseText = $('iframe')[0].contentDocument.body.textContent; var responseData = JSON.parse(responseText) || {}; if (responseData.isSuccess == true || responseData.code == 200) { //success } else { //error } });
How beforeSend improves user experience
SpringMVC environment Ajax asynchronous request JSON method
The above is the detailed content of How to implement file upload with Ajax and form+iframe (detailed explanation with pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the advent of the digital age, music platforms have become one of the main ways for people to obtain music. However, sometimes when we listen to songs, we find that there are no lyrics, which is very disturbing. Many people hope that lyrics can be displayed when listening to songs to better understand the content and emotions of the songs. QQ Music, as one of the largest music platforms in China, also provides users with the function of uploading lyrics, so that users can better enjoy music and feel the connotation of the songs. The following will introduce how to upload lyrics on QQ Music. first

1. Open Kugou Music and click on your profile picture. 2. Click the settings icon in the upper right corner. 3. Click [Upload Music Works]. 4. Click [Upload Works]. 5. Select the song and click [Next]. 6. Finally, click [Upload].

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Upload speed becomes very slow? I believe this is a problem that many friends will encounter when uploading things on their computers. If the network is unstable when using a computer to transfer files, the upload speed will be very slow. So how can I increase the network upload speed? Below, the editor will tell you how to solve the problem of slow computer upload speed. When it comes to network speed, we all know that the speed of opening web pages, download speed, and upload speed are also very critical. Especially some users often need to upload files to the network disk, so a fast upload speed will undoubtedly save you a lot of money. Less time, what should I do if the upload speed is slow? Below, the editor brings you pictures and texts on how to deal with slow computer upload speeds. How to solve the problem of slow computer upload speed? Click "Start--Run" or "Window key"

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

As long as the computer is equipped with a camera, it can take pictures, but some users still don't know how to take pictures and upload them. Now I will give you a detailed introduction to the method of taking pictures on the computer, so that users can upload the pictures wherever they want. How to take photos and upload them on a computer 1. Mac computer 1. Open Finder and click the application on the left. 2. After opening, click the Camera application. 3. Just click the photo button below. 2. Windows computer 1. Open the search box below and enter camera. 2. Then open the searched application. 3. Click the photo button next to it.
