This article mainly introduces how to use Thinkphp and ajaxFileUpload to implement asynchronous image transmission. This article provides detailed sample code, which is of reference value to everyone, let's take a look. This article mainly introduces how to use Thinkphp and ajaxFileUpload to implement asynchronous image transmission. This article provides detailed sample code, which is of reference value to everyone, let's take a look.
Preface
Before doing this project, the process of uploading images has always been directly using form submission for file transmission. this time, due to requirements, asynchronous transmission of images is required, although implementation is not difficult, after all, there are too many plug-ins, but it still wastes a long time for debugging. The reason is that I used the plug-in ajaxfileupload in ancient times and reported frequent errors when I first started using it, handler is not a function.
With a heavy mood, I tried Baidu and found the answer. I couldn't help but feel that the search engine is really powerful.
Solution
The reason for the above error is that jquery no longer uses the handler method from 1.9.0. the specific reason is unknown, so it can only be downloaded jQuery.extend({Add code manually:
handleError: function( s, xhr, status, e ){// If a local callback was specified, fire itif ( s.error ) {s.error.call( s.context || s, xhr, status, e );}// Fire the global callbackif ( s.global ) {(s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );}}
, Continue debug
$. Ajaxfileupload ({url: '', secureuri:'', fileElementId: '', --> enter the input file ID data: {}, dataType: 'json ', complete: function (data ){}})
The code does not report an error, but there are new problems. the returned data is always undefined. then I checked the debugging tool and returned the value. The format is correct, after thinking about it, go again .................. baidu,
Then we made two adjustments:
1. seteval('data = '+ data) ; Replacedata = jQuery.parseJSON(jQuery(data).text());
2. replace the complete method with success
OK. the data returned by the backend can be printed.
Summary
The above is a detailed description of the sample code for asynchronous image transmission using Thinkphp and ajaxFileUpload. For more information, see other related articles in the first PHP community!