图片用ajax的简单提交
编辑时间:2017-03-26 作者:金满斗 浏览量:2555 来源:原创

这段时间准备写个属于自己的CMS。刚好下雨,加班加点吧。有点急。

用后台模板的时候发现layui还是蛮漂亮的,但由于一些别的想法最终放弃了。还是用自己搞的一套吧,不过自己觉得layui里面的传图控件还是蛮好的。由于这次写的是CMS,里面涉及到配置文件里动态加图,单然最好就是ajax提交图了,网上看了下有的说为了安全图必须在表单里才能上传的。百度折腾了一下,放上来。照例放代码片段吧。

前台部分

codelayui.code

  1. <div class="page-container">
  2. <form class="form form-horizontal" id="form" action="{:url('system/system_save')}">
  3. <div id="tab-system" class="HuiTab">
  4. <div class="tabBar cl">
  5. {volist name="Think.config.CONFIG_GROUP_LIST" id="group"}
  6. <span>{$group}</span>
  7. {/volist}
  8. </div>
  9. {volist name="conftab" id="vo"}
  10. <div class="tabCon">
  11. {volist name="vo" id="voo"}
  12. <div class="row cl">
  13. <label class="form-label col-xs-4 col-sm-2">{$voo.title}:</label>
  14. <div class="formControls col-xs-8 col-sm-9">
  15. {switch name="voo.type"}
  16. {case value="0"}
  17. <input type="number" name ="config[{$voo.name}]" placeholder="{$voo.remark}" value="{$voo.value}" class="input-text">
  18. {/case}
  19. {case value="1"}
  20. <input type="text" name ="config[{$voo.name}]" placeholder="{$voo.remark}" value="{$voo.value}" class="input-text">
  21. {/case}
  22. {case value="2"}
  23. <textarea name="config[{$voo.name}]"  class="textarea" placeholder="{$voo.remark}" dragonfly="true" >{$voo.value}</textarea>
  24. {/case}
  25. {case value="3"}
  26. <textarea name="config[{$voo.name}]" cols="" rows="" class="textarea"  placeholder="{$voo.remark}" dragonfly="true" >{$voo.value}</textarea>
  27. {/case}
  28. {case value="4"}
  29. <select name="config[{$voo.name}]"  class="select" style="width:120px;">
  30. {volist name=":parse_config_attr($voo['extra'])" id="vo1"}
  31. <option value="{$key}" {eq name="voo.value" value="$key"}selected{/eq}>{$vo1}</option>
  32. {/volist}
  33. </select>
  34. {/case}
  35. {case value="5"}
  36. <input type="hidden" name ="config[{$voo.name}]"  value="{$voo.value}" id="val_{$voo.name}" >
  37. <img src="{$voo.value}" style="width: 100px;height: 50px; margin:20px;" id="img_{$voo.name}" >
  38. <a href="javascript:;" class="file" title="点击选择所要上传的图片" >上传图片
  39. <input type="file"   data-href="{$voo.name}" name="image[]" id="file_{$voo.name}" multiple="multiple"/>
  40. </a>
  41. {/case}
  42. {/switch}
  43. </div>
  44. </div>
  45. {/volist}
  46.   </div> 
  47. {/volist}
  48. </div>
  49. <div class="row cl">
  50. <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
  51. <button class="btn btn-primary radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 保存</button>
  52. <button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
  53. </div>
  54. </div>
  55. </form>
  56. </div>
  57. {/block}
  58. {block name="footerjs"}
  59. {/*请在下方写此页面业务相关的脚本*/ }
  60.  
  61.  
  62. <script type="text/javascript">
  63. $(function(){
  64. $.Huitab("#tab-system .tabBar span","#tab-system .tabCon","current","click","0");
  65.  
  66. function getObjectURL(file,field) {
  67.     var url = null;
  68.     if (window.createObjectURL != undefined) { // basic
  69.         $("#oldcheckpic_"+field).val("nopic");
  70.         url = window.createObjectURL(file);
  71.     } else if (window.URL != undefined) { // mozilla(firefox)
  72.         $("#oldcheckpic_"+field).val("nopic");
  73.         url = window.URL.createObjectURL(file);
  74.     } else if (window.webkitURL != undefined) { // webkit or chrome
  75.         $("#oldcheckpic_"+field).val("nopic");
  76.         url = window.webkitURL.createObjectURL(file);
  77.     }
  78.     return url;
  79. }
  80.  
  81. $("input[id^=file_]").change(function () {
  82.     var field=$(this).attr('data-href'),objUrl = getObjectURL(this.files[0],field);
  83.     if (objUrl) {
  84.         $("#img_"+field).attr("src", objUrl);
  85.     }
  86.     //创建FormData对象
  87.         var data = new FormData();
  88.         //为FormData对象添加数据
  89.         $.each($(this)[0].files, function(i, file) {
  90.             data.append('file', file);
  91.         });
  92.         $.ajax({
  93.             url:'{:url('api/upload/index',['catid'=>1])}',
  94.             type:'POST',
  95.             data:data,
  96.             cache: false,
  97.             contentType: false,    //不可缺
  98.             processData: false,    //不可缺
  99.             success:function(data){
  100.                 if(data.info){
  101.                     if (data.status==1) {
  102.                    
  103. $("#val_"+field).val(data.src);
  104.                     }else{
  105.                         layer.msg(data.info,{icon:2,time:3000});
  106.                     }  
  107.                 }else{
  108.                     layer.msg("服务器响应错误",{icon:5});
  109.                 }
  110.         }});
  111. });
  112. });
  113. </script>

这里前台在ajax传图前就可以取到图预览的,也是看的别人的代码。觉得不错。后台图上传部分就不贴了。跟thinkphp5里面的传图教程差不多的。

来说两句吧
  • 请先说点什么
    热门评论
    2556人参与,0条评论www