关于PHP+iFrame实现页面无需刷新的异步文件上传

发布时间:2020-07-16 16:16:04 作者:熊科泉
来源:网络 阅读:490
  1. 在iframe标签一般会指定其name特性以于标识;
    2. 在form表单中通过action(目标地址)和target(目标窗口,默认为_self)来确定提交的目的地;
    3. 将form中的target指向iframe的name,则可将表单提交到了隐藏框架iframe中;
    4. iframe里的内容实际上也是一个页面,其中的js里的parent对象指代父页面,即嵌入iframe的页面;
    5. PHP中用move_uploaded_file()函数来实现文件上传,$_FILES数组存储有上传文件的相关信息。


<form enctype="multipart/form-data" method="POST" target="upload" action="http://localhost/uploadFile.php" >
          <input type="file" name="uploadfile" />
          <input type="submit" />
</form>
<iframe name="upload" ></iframe>

和一般的<form>标签相比多了一个target属性罢了,用于指定标签页在哪里打开以及提交数据。

如果没有设置该属性,就会像平常一样在本页重定向打开action中的url。

而如果设置为iframe的name值,即"upload"的话,就会在该iframe内打开,因为CSS设置为隐藏,因而不会有任何动静。若将display:none去掉,还会看到服务器的返回信息。

另外贴一下自己组织的类。

function uploadFile()
{
    $file = $_FILES['inputpdf']['name'];
    $filetempname = $_FILES['inputpdf']['tmp_name'];
    $filelist = explode('.',$file);
    $type = end($filelist);
    if($type != 'pdf'){
        $return = array (
            'rsp' => 'fail',
            'res' => '请上传pdf文件!',
        );
        echo json_encode($return);exit;
    }
    //自己设置的上传文件存放路径
    $filePath = './public/pdf/';

    $contract_name = $file;
    $string_md5 = md5 (md5($contract_name).time());
    $front_string = substr ($string_md5 ,0 ,31 );
    $contract_url = 's'.$front_string.'.pdf';    //pdf名称

    $uploadfile = $filePath .$contract_url;//上传后的文件名地址
    //move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
    $result = move_uploaded_file($filetempname, $uploadfile);//假如上传到当前目录下

    if($result == true){
        $orders = app::get('b2c')->model('orders')->getList('contract_no', array('order_id'=>$_POST['order_id']));  //获取用户发票信息
        $contracts = app::get('b2c')->model('contract_list')->getList('*', array('contract_no'=>$orders[0]['contract_no']));  //获取用户默认收货地址

        $contract_no = $orders[0]['contract_no'];
        $delfile = $contracts[0]['contract_url'];
        $contracts[0]['contract_url'] = $uploadfile;
        $contracts[0]['contract_name'] = $contract_name;
        $contracts[0]['uptime'] = date('Y-m-d H:i:s',time());
      //  unset($contracts[0]['id']);
        $flag = app::get('b2c')->model('contract_list')->update($contracts[0],array('id'=> $contracts[0]['id']));
        if($flag){
            if(file_exists($delfile)){
                unlink($delfile);
            }
            $return = array (
                'rsp' => 'succ',
                'url' => "/paycenter-download-$contract_no.html",
                'res' => '上传成功!',
            );
            echo json_encode($return);exit;
        }
    }else{
        $return = array (
            'rsp' => 'fail',
            'res' => '上传失败!',
        );
        echo json_encode($return);exit;
    }

}


推荐阅读:
  1. 异步请求与异步刷新
  2. Vue页面刷新记住页面状态的实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 上传文件 iframe

上一篇:好程序员Java教程分享Java之包装类与常用类

下一篇:[脚本]Unity3D一些基本功能的脚本

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》