Home Web Front-end JS Tutorial jQuery+formdata creates upload progress effects (with step code)

jQuery+formdata creates upload progress effects (with step code)

Apr 26, 2018 am 11:20 AM
special effects schedule

This time I will bring you jQuery formdata to make upload progress special effects (with step codes). What are the precautions for jQuery formdata to make upload progress special effects? . Here is a practical case. Let’s take a look. .

Problem List

1. JQUERY.AJAX does not monitor the ONPROGRESS event of upload progress.

2. XMLHTTPREQUEST (XHR) cross-domain

Question Answer

1. JQUERY does not provide an interface for the ONPROGRESS event, and the native XHR object must be found from other interfaces.

jQuery.ajax() returns the jqXHR object. jqXHR imitates XHR (native), but does not imitate all methods and attributes that implement XHR (such as: .upload), even if jqXHR adds a unique method (such as: .promise()). So jqXHR is not a superset of XHR.

//下面是截取jQ内部的源码,$.ajax();返回的就是这个jqXHR(伪造XMLHttpRequest)
// Fake xhr
 jqXHR = {
  readyState: 0,
Copy after login

The upload attribute of XHR points to XMLHttpRequestUpload (IE10 is XMLHttpRequestEventTarget), and the onprogress event of this object can monitor the upload progress. Since jQ does not provide an API for this function, some data upload methods of jQ use XHR, so we can find XHR from other APIs. Binding the onprogress event before XHR sends data can implement the upload progress function.

I found two properties related to XHR from the OPTIONS parameter configuration:

-XHR: callback creates XMLHTTPREQUEST object.

The return value of xhr() is XHR, which is provided for jQ to use, that is, this XHR is used to send data. We can create a callback function through xhr to overwrite it, also return XHR, but bind the onprogress event here.

//jQ源码
// Get a new xhr
var handle, i,
 xhr = s.xhr();//[回调]在这里,下面是open方法
// Open the socket
// Passing null username, generates a login popup on Opera (#2865)
if ( s.username ) {
 xhr.open( s.type, s.url, s.async, s.username, s.password );
} else {
 xhr.open( s.type, s.url, s.async );
}
Copy after login

So we should do this:

$.ajax({
 //.....
 xhr: function() {
  var xhr = $.ajaxSettings.xhr();
  //绑定上传进度的回调函数
  xhr.upload.addEventListener('progress', progress, false);
  return xhr;//一定要返回,不然jQ没有XHR对象用了
 }
});
Copy after login

- XHRFIELDS: A pair of "file name-file value" mapping, used to set the native XHR object.

The xhrFields attribute points to the XHR created internally by jQ. We can obtain the XMLHttpRequest based on xhrFields. Since the value of xhrFields can only be a json object, it cannot be obtained in the following way.

//错误例子
$.ajax({
 //......
 xhrFields: {
  upload.onprogress: function() {
   //语法错误
  }
 }
});
Copy after login

We can use XHR's onsendstart event, as follows:

$.ajax({
 //......
 xhrFields: {
  onsendstart: function() {
   //this是指向XHR
   this.upload.addEventListener('progress', progress, false);
  }
 }
});
Copy after login

2. XMLHTTPREQUESTⅡ(XHR) supports cross-domain, but requires background permission.

//后台需发送头部验证
if($_REQUEST['cros']) {
 header("Access-Control-Allow-Origin:请求的域名");
}
Copy after login

According to the interface provided by the background, I need to add a parameter cros. But when I submitted this parameter with the file, it prompted a cross-domain restriction. Finally, put this parameter in the url.

It turns out that XHR has two cross-domain requests. The first is a verification request. The browser automatically issues an options request based on the request destination address. If passed, a customized post request can be issued. So put the parameters in the post request. The first request does not have the cros parameter, that is, it cannot pass.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to create a menu link button with the jQuery EasyUI plug-in

Detailed explanation of the use of JQuery's jqURL plug-in

The above is the detailed content of jQuery+formdata creates upload progress effects (with step code). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to use Vue to implement pop-up window effects How to use Vue to implement pop-up window effects Sep 22, 2023 am 09:40 AM

How to use Vue to implement pop-up window effects requires specific code examples. In recent years, with the development of web applications, pop-up window effects have become one of the commonly used interaction methods among developers. As a popular JavaScript framework, Vue provides rich functions and ease of use, and is very suitable for implementing pop-up window effects. This article will introduce how to use Vue to implement pop-up window effects and provide specific code examples. First, we need to create a new Vue project using Vue's CLI tool. open end

How to use Vue to implement full-screen mask effects How to use Vue to implement full-screen mask effects Sep 19, 2023 pm 04:14 PM

How to use Vue to implement full-screen masking effects. In web development, we often encounter scenarios that require full-screen masking, such as displaying a masking layer when loading data to prevent users from performing other operations, or in some special scenarios. Use a mask layer to highlight an element. Vue is a popular JavaScript framework that provides convenient tools and components to achieve various effects. In this article, I will introduce how to use Vue to achieve the effect of full-screen masking, and provide some specific code examples. At first, we

How to use Vue to implement sidebar effects How to use Vue to implement sidebar effects Sep 19, 2023 pm 02:00 PM

How to use Vue to implement sidebar effects Vue is a popular JavaScript framework. Its simplicity, ease of use, and flexibility enable developers to quickly build interactive single-page applications. In this article, we will learn how to use Vue to implement a common sidebar effect, and provide specific code examples to help us understand better. Create a Vue project First, we need to create a Vue project. You can use the VueCLI (command line interface) provided by Vue, which can quickly generate

Implement card flipping effects in WeChat mini programs Implement card flipping effects in WeChat mini programs Nov 21, 2023 am 10:55 AM

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: <!--index.wxml-->&l

HTML, CSS and jQuery: Techniques for achieving image folding and expanding effects HTML, CSS and jQuery: Techniques for achieving image folding and expanding effects Oct 24, 2023 am 11:05 AM

HTML, CSS and jQuery: An introduction to techniques for implementing image folding and expanding special effects. In web design and development, we often need to implement some dynamic special effects to increase the attractiveness and interactivity of the page. Among them, the image folding and unfolding effect is a common but interesting technique. Through this special effect, we can make the image fold or expand under the user's operation to show more content or details. This article will introduce how to use HTML, CSS and jQuery to achieve this effect, with specific code examples. realize thoughts

How to use Vue to implement progress bar effects How to use Vue to implement progress bar effects Sep 19, 2023 am 09:22 AM

How to use Vue to implement progress bar effects The progress bar is a common interface element that can be used to display the completion of a task or operation. In the Vue framework, we can implement special effects of the progress bar through some simple code. This article will introduce how to use Vue to implement progress bar effects and provide specific code examples. Create a Vue component First, we need to create a Vue component to implement the progress bar function. In Vue, components are reusable and can be used in multiple places. Create a file called Pro

Steps to turn off mouse track effects in Windows 10 Steps to turn off mouse track effects in Windows 10 Dec 31, 2023 pm 09:53 PM

When we use the win10 system, we can make many personalized settings, including mouse track special effects. However, many users do not know how to turn off the mouse track special effects in win10. For this reason, we have provided detailed methods. How to turn off the mouse track effects in Windows 10: 1. First, right-click on a blank space on the desktop, and then click "Personalize". 2. Then click "Theme" on the left and select "Mouse Cursor" on the right. 3. After entering the properties, you can see and select "Pointer Options". 4. Then scroll down to see the visibility, and the √ is checked at this time. 5. Uncheck, then click Apply and OK.

How to use Vue to implement video player special effects How to use Vue to implement video player special effects Sep 20, 2023 pm 03:43 PM

How to use Vue to implement video player special effects Summary: This article will introduce how to use the Vue.js framework to implement a video player with various special effects. We will use Vue directives and components to implement play/pause buttons, progress bars, volume controls, and full screen functionality. At the same time, we will also add some animation effects to enhance the user experience. Different special effects will be introduced in detail below, including code examples. Play/pause button effects: It is very simple to use Vue instructions to implement play/pause button effects. first,

See all articles