uploader-image.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var server="http://demo.dcloud.net.cn/helloh5/uploader/upload.php";
  2. var files=[];
  3. // 上传文件
  4. function upload(){
  5. if(files.length<=0){
  6. plus.nativeUI.alert("没有添加上传文件!");
  7. return;
  8. }
  9. outSet("开始上传:")
  10. var wt=plus.nativeUI.showWaiting();
  11. var task=plus.uploader.createUpload(server,
  12. {method:"POST"},
  13. function(t,status){ //上传完成
  14. if(status==200){
  15. outLine("上传成功:"+t.responseText);
  16. plus.storage.setItem("uploader",t.responseText);
  17. var w=plus.webview.create("uploader_ret.html","uploader_ret.html",{scrollIndicator:'none',scalable:false});
  18. w.addEventListener("loaded",function(){
  19. wt.close();
  20. w.show("slide-in-right",300);
  21. },false);
  22. }else{
  23. outLine("上传失败:"+status);
  24. wt.close();
  25. }
  26. }
  27. );
  28. task.addData("client","HelloH5+");
  29. task.addData("uid",getUid());
  30. for(var i=0;i<files.length;i++){
  31. var f=files[i];
  32. task.addFile(f.path,{key:f.name});
  33. }
  34. task.start();
  35. }
  36. // 拍照添加文件
  37. function appendByCamera(){
  38. plus.camera.getCamera().captureImage(function(p){
  39. appendFile(p);
  40. });
  41. }
  42. // 从相册添加文件
  43. function appendByGallery(){
  44. plus.gallery.pick(function(p){
  45. appendFile(p);
  46. });
  47. }
  48. // 从相册中选择多张图片
  49. function galleryImgs(){
  50. // 从相册中选择图片
  51. console.log("从相册中选择多张图片:");
  52. plus.gallery.pick( function(e){
  53. for(var i in e.files){
  54. appendFile(e.files[i]);
  55. }
  56. }, function ( e ) {
  57. console.log( "取消选择图片" );
  58. },{filter:"image",multiple:true});
  59. }
  60. // 添加文件
  61. var index=1;
  62. function appendFile(p){
  63. var fe=document.getElementById("files");
  64. var li=document.createElement("li");
  65. var n=p.substr(p.lastIndexOf('/')+1);
  66. li.innerText=n;
  67. fe.appendChild(li);
  68. files.push({name:"uploadkey"+index,path:p});
  69. index++;
  70. empty.style.display="none";
  71. //预览
  72. var imgList = document.getElementById("img_list");
  73. var img = document.createElement("img");
  74. img.src = p;
  75. imgList.appendChild(img);
  76. img.width = 50;
  77. img.height = 50;
  78. }
  79. // 产生一个随机数
  80. function getUid(){
  81. return Math.floor(Math.random()*100000000+10000000).toString();
  82. }