How to handle asynchronous file upload and ajax batch requests using the jquery plug-in ajaxfileuploa

Source: Internet
Author: User

If you encounter these problems due to work requirements, share these problems and solutions.

As we all know, jquery's ajax is quite simple, practical, and compatible with various browsers, but it does not know that its native does not support ajax File Upload. On the internet Baidu search plug-in ajaxfileupload. js, paste the instance code:

$ ("# BtnSubmit "). click (function () {if ($ ("input [type = file]"). val () = '') {alert ('select the uploaded file'); return false;} var str = $ (" input [type = file] "). val (); var len = str. lastIndexOf (". "); var type = str. substring (len + 1, str. length); type = type. toLowerCase (); if (type! = "Csv") {alert ('only csv files can be uploaded '); return false ;}$ ("# uploading "). hide (); $ ("# loading "). show (); // submit data asynchronously first $. ajaxFileUpload ({url: 'Member _ distribution. action. import. php ', secureuri: false, fileElementId: 'file1', ype: 'text', async: false, success: function (up_count, status) {if (! IsNaN (up_count) & up_count> 0) {count_y = up_count; check_distribution (up_count) ;}else {alert (up_count); return false ;}}}); // alert ('upload takes some time. Please wait until the prompt box is displayed successfully! ');})

The above is the code for asynchronous file upload. However, the plug-in is based on jquery components and requires support from jquery, but not in some versions, if you find an error such as "handleError", please click to download this plug-in to overwrite

Ajax asynchronous batch requests are designed based on your needs. If you want to request requests at a fixed time, this is very easy, you can simply use the function that runs once every second in JS. This is not enough, because we all know, but the problem I encountered is that I only know the number of requests, however, the number of requests may be different. At this time, we should consider this problem with another idea,

I would like to say that my current idea was like this, because I knew the number of requests required, and then made requests in a loop. After debugging under Firefox is normal, I found incompatibility in IE and threw all your requests to the browser for one-time processing. As a result, 50 requests will crash immediately, in this way, only other methods can be used to solve such problems.

As a matter of fact, let's just think about it. This means we can write a request as a function, and then use a recursive method to process it until the condition is true, so that the request is terminated. Not to mention, paste the Code:

Function ajax_fun () {var OBJ ={}; obj. groupname = $ ("input [name = groupname]"). val (); obj. counts = $ ("input [name = counts]"). val (); counts_y = $ ("input [name = counts]"). val (); obj. password = $ ("input [name = PASSWORD]"). val (); if ($ ("body "). data ("_ counts") = undefined) {$ ("body "). data ("_ counts", obj. counts) ;}$. post ("member_distribution.batch.action.php", OBJ, function (MSG) {var COUNT = $ ("body "). data ("_ counts"); If (MSG = 1) {$ ("# num_s" ).html (now_num); If (count! = 1) {count --; now_num ++; $ ("body "). data ("_ counts", count); ajax_fun () ;}if (counts_y = now_num) {// alert ('generate successfully '+ now_num + 'data ') $ ("# uploading "). show (); $ ("# loading "). hide (); url = "member_distribution.list.php"; window. location. href = URL; return false ;}} else {alert (MSG); $ ("# uploading "). show (); $ ("# loading "). hide (); Return false ;}});}

We can see that there is another function in ajax_fun () in it, and it can only be terminated after the condition is set up, so that I can implement my current idea.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.