{"action":"created","issue":{"url":"https://api.github.com/repos/fex-team/webuploader/issues/185","labels_url":"https://api.github.com/repos/fex-team/webuploader/issues/185/labels{/name}","comments_url":"https://api.github.com/repos/fex-team/webuploader/issues/185/comments","events_url":"https://api.github.com/repos/fex-team/webuploader/issues/185/events","html_url":"https://github.com/fex-team/webuploader/issues/185","id":33951903,"number":185,"title":"应用于移动端注意事项","user":{"login":"2betop","id":2698393,"avatar_url":"https://avatars.githubusercontent.com/u/2698393?v=3","gravatar_id":"","url":"https://api.github.com/users/2betop","html_url":"https://github.com/2betop","followers_url":"https://api.github.com/users/2betop/followers","following_url":"https://api.github.com/users/2betop/following{/other_user}","gists_url":"https://api.github.com/users/2betop/gists{/gist_id}","starred_url":"https://api.github.com/users/2betop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/2betop/subscriptions","organizations_url":"https://api.github.com/users/2betop/orgs","repos_url":"https://api.github.com/users/2betop/repos","events_url":"https://api.github.com/users/2betop/events{/privacy}","received_events_url":"https://api.github.com/users/2betop/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/fex-team/webuploader/labels/faq","name":"faq","color":"006b75"},{"url":"https://api.github.com/repos/fex-team/webuploader/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":40,"created_at":"2014-05-21T05:16:41Z","updated_at":"2015-06-06T01:45:24Z","closed_at":null,"body":"webuploader 是满足 ios 6+, android 4+ 的。\r\n\r\n目前移动端有几个重要的 bug 在此列出来以免大家踩坑。\r\n\r\n1. 上传请求内容为空，这是 android 4 的一个bug，只要 file 的 blob数据修改过，通过 xhr2 去发送就有这个问题。 详情：https://code.google.com/p/android/issues/detail?id=39882\r\n\r\n  解决方案： 发送的时候采用二进制方案，即 把 sendAsBinary 设置成true, 通过这种方式可以避免这个问题。后端接受需要小改动一下。\r\n2. jpeg 压缩后体积反而变大了，这是 android 的另外一个 bug, canvas.toDataUrl 不支持 jpeg编码，实际上采用的 png 编码。详情： https://code.google.com/p/android/issues/detail?id=39885\r\n    \r\n    解决方案：引入了一个 jpeg js压缩库，在不支持的的情况下采用此库。但是此库默认没有打包进来，需要自行配置打包方案，将 runtime/html5/androidpatch.js 打包进来。"},"comment":{"url":"https://api.github.com/repos/fex-team/webuploader/issues/comments/109498382","html_url":"https://github.com/fex-team/webuploader/issues/185#issuecomment-109498382","issue_url":"https://api.github.com/repos/fex-team/webuploader/issues/185","id":109498382,"user":{"login":"relaxgo","id":6234876,"avatar_url":"https://avatars.githubusercontent.com/u/6234876?v=3","gravatar_id":"","url":"https://api.github.com/users/relaxgo","html_url":"https://github.com/relaxgo","followers_url":"https://api.github.com/users/relaxgo/followers","following_url":"https://api.github.com/users/relaxgo/following{/other_user}","gists_url":"https://api.github.com/users/relaxgo/gists{/gist_id}","starred_url":"https://api.github.com/users/relaxgo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/relaxgo/subscriptions","organizations_url":"https://api.github.com/users/relaxgo/orgs","repos_url":"https://api.github.com/users/relaxgo/repos","events_url":"https://api.github.com/users/relaxgo/events{/privacy}","received_events_url":"https://api.github.com/users/relaxgo/received_events","type":"User","site_admin":false},"created_at":"2015-06-06T01:45:24Z","updated_at":"2015-06-06T01:45:24Z","body":"对于android上传内容为空的bug可以尝试以下代码\r\n```\r\nvar\r\n    // Android native browser uploads blobs as 0 bytes, so we need a test for that\r\n    needsFormDataShim = (function () {\r\n        var bCheck = ~navigator.userAgent.indexOf('Android')\r\n                        && ~navigator.vendor.indexOf('Google')\r\n                        && !~navigator.userAgent.indexOf('Chrome');\r\n\r\n        return bCheck && navigator.userAgent.match(/AppleWebKit\\/(\\d+)/).pop() <= 534;\r\n    })(),\r\n\r\n    // Test for constructing of blobs using new Blob()\r\n    blobConstruct = !!(function () {\r\n        try { return new Blob(); } catch (e) {}\r\n    })(),\r\n\r\n    // Fallback to BlobBuilder (deprecated)\r\n    XBlob = blobConstruct ? window.Blob : function (parts, opts) {\r\n        var bb = new (window.BlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder);\r\n        parts.forEach(function (p) {\r\n            bb.append(p);\r\n        });\r\n\r\n        return bb.getBlob(opts ? opts.type : undefined);\r\n    };\r\n\r\nfunction FormDataShim () {\r\n    var\r\n        // Store a reference to this\r\n        o = this,\r\n\r\n        // Data to be sent\r\n        parts = [],\r\n\r\n        // Boundary parameter for separating the multipart values\r\n        boundary = Array(21).join('-') + (+new Date() * (1e16*Math.random())).toString(36),\r\n\r\n        // Store the current XHR send method so we can safely override it\r\n        oldSend = XMLHttpRequest.prototype.send;\r\n\r\n    this.append = function (name, value, filename) {\r\n        parts.push('--' + boundary + '\\nContent-Disposition: form-data; name=\"' + name + '\"');\r\n\r\n        if (value instanceof Blob) {\r\n            parts.push('; filename=\"'+ (filename || 'blob') +'\"\\nContent-Type: ' + value.type + '\\n\\n');\r\n            parts.push(value);\r\n        }\r\n        else {\r\n            parts.push('\\n\\n' + value);\r\n        }\r\n        parts.push('\\n');\r\n    };\r\n\r\n    // Override XHR send()\r\n    XMLHttpRequest.prototype.send = function (val) {\r\n        var fr,\r\n            data,\r\n            oXHR = this;\r\n\r\n        if (val === o) {\r\n            // Append the final boundary string\r\n            parts.push('--' + boundary + '--');\r\n\r\n            // Create the blob\r\n            data = new XBlob(parts);\r\n\r\n            // Set up and read the blob into an array to be sent\r\n            fr = new FileReader();\r\n            fr.onload = function () { oldSend.call(oXHR, fr.result); };\r\n            fr.onerror = function (err) { throw err; };\r\n            fr.readAsArrayBuffer(data);\r\n\r\n            // Set the multipart content type and boudary\r\n            this.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);\r\n            XMLHttpRequest.prototype.send = oldSend;\r\n        }\r\n        else {\r\n            oldSend.call(this, val);\r\n        }\r\n    };\r\n}\r\n```\r\n\r\n[原文链接](http://stackoverflow.com/questions/15639070/empty-files-uploaded-in-android-native-browser/28809955#28809955)"}}