QeePHP中上传多个文件的基础写法

 if (request_is_post()){

            $uploader = new Helper_Uploader();          
            $allFiles=$uploader->allFiles();
            if (count($allFiles)>0){
                foreach ($allFiles as $file){
                    /* @var  $file Helper_Uploader_File */
                    $file_name = $file->filename();
                    //文件的后缀名
                    $file_type = $file->extname();
                    //不包含后缀的文件名
                    $file_front = basename($file_name,'.'.$file_type);
                    $waybill = Waybill::find('waybill_code = ?',$file_front)->getOne();
                    if($waybill->waybill_id!=null){
                        $waybill_id = $waybill->waybill_id;
                    }else {                       
                    return $this->_redirectMessage("扫描文件".$file_front."上传失败! 
                    6秒中之后将自动跳回... ...",
                    "上传扫描件失败! 运单表中无对应文件名为 ".$file_front." 的主运单号!", 
                    url("scannedmanage/index") ,6);
                    }
                    //文件上传路径
                    $uploadtmp_dir = _INDEX_DIR_.'/public/upload/scanningcopy/';
                    Helper_Filesys::mkdirs($uploadtmp_dir);
                    $fullname = $uploadtmp_dir . $file_name;
                    $file->move($fullname);
                    //数据保存到数据库
                    $files = new File();
                    $files->waybill_id = $waybill_id;
                    $files->file_name = $file_name;
                    $files->file_path = $uploadtmp_dir;
                    $files->operator = "Alan";
                    $files->remark = request('remark');
                    $files->save();
                }
                return $this->_redirect(url("scannedmanage/index"));
            }else {
                return $this->_redirect(url("scannedmanage/index"));
            }
        }

客户端页面写法:

<form action="<?php echo url('scannedmanage/upload2');?>" method="post" enctype="multipart/form-data">
        <label>备注:</label>
        <input type="text" name="remark" id="remark" />
        <label>选择文件:</label>
        <div>
            <input type="text" name="filepath" id="filepath">
            <?php echo Q::control('upload', 'files[]', array('multiple'=>'multiple','accept'=>'image/*'))?>
        </div>
        <input type="submit" class="btn" name="submit" value="上传"
            onclick="return doFormCheck();" />
</form>

说明:注意QeePHP框架自带的upload组件中关于支持多文件上传和只接受图片的写法!还有一点就是组件的name要为数组类型的,如此处的files[ ]!

拖拽上传多个文件:

客户端页面直接将要上传限定的区域包含到一个id名为dropArea的div中即可!

脚本部分:

<script type="text/javascript">
/**
 * 拖动事件
 */
var dropHandler = function(e) {
    fileList = e.dataTransfer.files;
    for(var i=0;i<fileList.length;i++){
    if (fileList.length == 0) {
        return;
    }
    
    if (fileList[i].type.indexOf('image') === -1) {
        alert("请上传图片格式文件");
        return;
    }
    var xhr = new XMLHttpRequest();
    var upload = xhr.upload;
    upload.addEventListener("load", uploadSucceed, false);
    xhr.open("post",'<?php echo url('scannedmanage/upload1')?>&file_name=' + fileList[i].name, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; boundary=' + new Date().getTime());
    xhr.send(fileList[i]);
    }
};

/**
 * 拖动后刷新页面
 */
function uploadSucceed(event) {
    window.location.reload();
}
</script>

注意部分:下述一行Ajax请求代码,我原以为在QeePHP框架中通过控制器、动作这种构成路径的形式是传不到数据到指定的控制器方法中的,以为得采用绝对路径的方式来指定到具体路径下的页面,但是实际的情况是,这种写法同样能传递数据到目标文件!

xhr.open("post",'<?php echo url('scannedmanage/upload1')?>&file_name=' + fileList[i].name, true);


转载于:https://my.oschina.net/zhouyang2021/blog/376521

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值