博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ajaxFileUpload+ThinkPHP+jqGrid 图片上传与显示
阅读量:6997 次
发布时间:2019-06-27

本文共 9466 字,大约阅读时间需要 31 分钟。

hot3.png

  • jqgrid上要显示图片和上传图片的列,格式如下:
{label:'图片',name:'icon',index:'icon',autowidth:true,formatter:alarmFormatter,editable:true,edittype:'custom', editoptions:{custom_element: ImgUpload, custom_value:GetImgValue}},

注意:edittype要为custom 也就是自定义编辑格式.

editoptions:{custom_element: ImgUpload, custom_value:GetImgValue}}

  • jqgrid 的列表里显示图片用到的 js function 此处与图片的上传没关系.
function alarmFormatter(cellvalue, options, rowdata){        return ''    }
  • 下面为上传用到的 js 文件 upload.js
/** 4. 自定义文件上传列 5. @param value 6. @param editOptions 7. @returns {*|jQuery|HTMLElement} 8. @constructor */function ImgUpload(value, editOptions) {    var span = $("");    var hiddenValue = $("",{type:"hidden", val:value, name:"fileName", id:"fileName"});    var image = $("",{name:"uploadImage", id:"uploadImage",value:'',style:"display:none;width:80px;height:80px"});    var el = document.createElement("input");    el.type = "file";    el.id = "imgFile";    el.name = "imgFile";    el.onchange = UploadFile;    span.append(el).append(hiddenValue).append(image);    return span;}/** 9. 调用 ajaxFileUpload 上传文件 10. @returns {boolean} 11. @constructor */function UploadFile() {    $.ajaxFileUpload({        url : 'index.php/Home/upload/upload',        type : 'POST',        secureuri:false,        fileElementId: 'imgFile',        dataType : 'json',        success: function(data,status){            //显示图片            $("#fileName").val(data.id);            $("#uploadImage").attr("src","index.php/Home/download/download?id=" + data.id);            $("#uploadImage").show();            $("#imgFile").hide()        },        error: function(data, status, e){            alert(e);        }    });    return false;}/** 12. icon 编辑的时候该列对应的实际值 13. @param elem 14. @param sg 15. @param value 16. @returns {*|jQuery} 17. @constructor */function GetImgValue(elem, sg, value){    return $(elem).find("#fileName").val();}
  • 下面为ThinkPHP上传代码部分
maxSize = 4194304; //4MB $upload->exts = array('jpg','gif','png','jpeg'); $upload->rootPath = C("FILE_PATH"); //根目录 $upload->autoSub = false;// $upload->savePath = C("FILE_PATH"); //附件上传目录, 相对于根目录// $upload->saveName = array('uniqid',''); //上传文件 $info = $upload->uploadOne($_FILES['imgFile']); if(!$info){ //上传错误提示信息 $this->e($upload->getError()); $this->returnError($upload->getError()); }else{ //存数据库部分 $photo = D('photo'); $photo->name = $info['savename']; $photo->realName = $info['name']; $photo->suffix = $info['ext']; $photo->size = $info['size']; //...省略部分代码 $id = $photo->add();// $this->ajaxReturn(array('msg'=>$id),"JSON"); echo json_encode(array('id'=>$id)); } }}

因为 thinkphp 自带的 ajaxReturn 返回的数据带有pre标签,会导致ajaxFIleUpload 解析不了,所以用了原生的 echo json_encode() 函数

  • ajaxFileUpload.js
jQuery.extend({    createUploadIframe: function(id, uri)    {        //create frame        var frameId = 'jUploadFrame' + id;        var iframeHtml = '

转载于:https://my.oschina.net/zcqshine/blog/596062

你可能感兴趣的文章