这是我想出的解决此问题的方法。$ .ajax()调用允许提供回调以生成XHR。我只是在调用请求之前生成一个请求,进行设置,然后创建一个闭包以在$
.ajax()需要它时将其返回。如果他们只是通过jqxhr授予访问权限,那会容易得多,但事实并非如此。
var reader = new FileReader();reader.onloadend = function (e) { var xhr, provider; xhr = jQuery.ajaxSettings.xhr(); if (xhr.upload) { xhr.upload.addEventListener('progress', function (e) { // ... }, false); } provider = function () { return xhr; }; // Leave only the actual base64 component of the 'URL' // Sending as binary ends up mangling the data somehow // base64_depre() on the PHP side will return the valid file. var data = e.target.result; data = data.substr(data.indexOf('base64') + 7); $.ajax({ type: 'POST', url: 'http://example.com/upload.php', xhr: provider, dataType: 'json', success: function (data) { // ... }, error: function () { // ... }, data: { name: file.name, size: file.size, type: file.type, data: data, } }); }; reader.readAsDataURL(file);


