file-api.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. (function (exports) {
  2. var fileAPI = {
  3. //文件流上传图片
  4. uploadImg: function (data) {
  5. return httpRequest.httpPostContent("/dfs/api/v1.0/fastDfs/upload?creator="+data.creator+"&objectId="+data.objectId,{data: data.files, processData : false, contentType : false})
  6. },
  7. getBase64Image: //将图片压缩转成base64
  8. function (imgSrc,width) {
  9. return new Promise(function (resolve, reject) {
  10. var canvas = document.createElement("canvas");
  11. var img = new Image;
  12. img.src = imgSrc;
  13. if(width){
  14. img.width = width;
  15. img.height = width;
  16. }
  17. var ctx = canvas.getContext("2d");
  18. img.onload = function () {
  19. canvas.width = img.width; /*设置新的图片的宽度*/
  20. canvas.height = img.height; /*设置新的图片的长度*/
  21. ctx.drawImage(img, 0, 0, img.width, img.height); /*绘图*/
  22. var dataURL = canvas.toDataURL("image/png", 0.8);
  23. resolve(dataURL.replace("data:image/png;base64,", ""));
  24. }
  25. })
  26. },
  27. zipImg:function(src,isSmall){
  28. return new Promise(function(resolve,redirect){
  29. var aImg = src.split("/");
  30. var imgName = aImg[aImg.length - 1];
  31. plus.zip.compressImage({
  32. src: src,
  33. dst: aImg+imgName,
  34. quality: 20,
  35. overwrite: true,
  36. width:'50%'
  37. }, function(succ) {
  38. var url = succ.target;
  39. var size = succ.size;
  40. var width = succ.width;
  41. var height = succ.height;
  42. resolve(succ)
  43. }, function(err) {
  44. console.error("压缩失败:" + err.message);
  45. redirect(err)
  46. if(err.message == "文件不存在") {
  47. mui.toast(err.message);
  48. plus.nativeUI.closeWaiting();
  49. plus.webview.currentWebview().reload();
  50. }
  51. });
  52. })
  53. }
  54. }
  55. exports.fileAPI = fileAPI;
  56. })(window)