upload.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //图片上传 (uploadImageArray = [{"name":"xxx","base64":"xxx"}])
  2. function uploadImg(uploadImageArray, qiniuToken) {
  3. if (uploadImageArray && uploadImageArray.length > 0 && qiniuToken) {
  4. //上传,返回地址
  5. var putExtra = {
  6. fname: "",
  7. params: {},
  8. mimeType: [] || null
  9. };
  10. var config = {
  11. useCdnDomain: true
  12. };
  13. window.WinSuccCount = 0;
  14. window.WinUploadImgCount = uploadImageArray.length;
  15. for (var i = 0; i < uploadImageArray.length; i++) {
  16. doUpload(uploadImageArray[i].name, uploadImageArray[i].base64.replace('data:image/png;base64,', ''), qiniuToken);
  17. }
  18. }
  19. }
  20. //图片上传七牛云
  21. function doUpload(name, base64, qiniuToken) {
  22. //图片上传(base64模式直传)
  23. var urlkey = toBase64(name); //自定义文件名必须是base64格式的
  24. var url = "http://upload.qiniup.com/putb64/-1/key/" + urlkey; //非华东空间需要根据注意事项-修改上传域名(upload.qiniup.com)
  25. var xhr = new XMLHttpRequest();
  26. xhr.onreadystatechange = function () {
  27. if (xhr.readyState == 4) {
  28. //返回信息
  29. console.log(xhr.responseText);
  30. //检测是否上传完毕,提交请求给服务器
  31. ++window.WinSuccCount;
  32. if (window.WinUploadImgCount == window.WinSuccCount) {
  33. reqCheckServer();
  34. window.WinSuccCount = 0;
  35. window.WinUploadImgCount = 0;
  36. }
  37. }
  38. }
  39. xhr.open("POST", url, true);
  40. xhr.setRequestHeader("Content-Type", "application/octet-stream");
  41. xhr.setRequestHeader("Authorization", "UpToken " + qiniuToken);
  42. xhr.send(base64);
  43. }
  44. //提交作业批改(php)
  45. function reqCheckServer() {
  46. if (window.checkJson) {
  47. // prompt("最终结果", window.checkJson);
  48. jQuery.ajax({
  49. type: "POST",
  50. url: config.checkURL,
  51. data: { "param": window.checkJson },
  52. success: function (res) {
  53. location.href = config.callbackURL;
  54. console.log(res);
  55. }
  56. });
  57. jQuery("#maskDiv").hide();
  58. }
  59. }
  60. function toBase64(data) {
  61. var toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  62. var base64Pad = '=';
  63. var result = '';
  64. var length = data.length;
  65. var i;
  66. for (i = 0; i < (length - 2); i += 3) {
  67. result += toBase64Table[data.charCodeAt(i) >> 2];
  68. result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];
  69. result += toBase64Table[((data.charCodeAt(i + 1) & 0x0f) << 2) + (data.charCodeAt(i + 2) >> 6)];
  70. result += toBase64Table[data.charCodeAt(i + 2) & 0x3f];
  71. }
  72. if (length % 3) {
  73. i = length - (length % 3);
  74. result += toBase64Table[data.charCodeAt(i) >> 2];
  75. if ((length % 3) == 2) {
  76. result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];
  77. result += toBase64Table[(data.charCodeAt(i + 1) & 0x0f) << 2];
  78. result += base64Pad;
  79. } else {
  80. result += toBase64Table[(data.charCodeAt(i) & 0x03) << 4];
  81. result += base64Pad + base64Pad;
  82. }
  83. }
  84. return result;
  85. }
  86. //base64转blob
  87. function base64ToBlob(base64, contentType, sliceSize) {
  88. contentType = contentType || '';
  89. sliceSize = sliceSize || 512;
  90. var byteCharacters = atob(base64);
  91. var byteArrays = [];
  92. for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
  93. var slice = byteCharacters.slice(offset, offset + sliceSize);
  94. var byteNumbers = new Array(slice.length);
  95. for (var i = 0; i < slice.length; i++) {
  96. byteNumbers[i] = slice.charCodeAt(i);
  97. }
  98. var byteArray = new Uint8Array(byteNumbers);
  99. byteArrays.push(byteArray);
  100. }
  101. var blob = new Blob(byteArrays, { type: contentType });
  102. return blob;
  103. }