溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

關(guān)于PHP+iFrame實(shí)現(xiàn)頁(yè)面無(wú)需刷新的異步文件上傳

發(fā)布時(shí)間:2020-07-16 16:16:04 來(lái)源:網(wǎng)絡(luò) 閱讀:501 作者:熊科泉 欄目:web開(kāi)發(fā)
  1. 在iframe標(biāo)簽一般會(huì)指定其name特性以于標(biāo)識(shí);
    2. 在form表單中通過(guò)action(目標(biāo)地址)和target(目標(biāo)窗口,默認(rèn)為_(kāi)self)來(lái)確定提交的目的地;
    3. 將form中的target指向iframe的name,則可將表單提交到了隱藏框架iframe中;
    4. iframe里的內(nèi)容實(shí)際上也是一個(gè)頁(yè)面,其中的js里的parent對(duì)象指代父頁(yè)面,即嵌入iframe的頁(yè)面;
    5. PHP中用move_uploaded_file()函數(shù)來(lái)實(shí)現(xiàn)文件上傳,$_FILES數(shù)組存儲(chǔ)有上傳文件的相關(guān)信息。


<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>標(biāo)簽相比多了一個(gè)target屬性罷了,用于指定標(biāo)簽頁(yè)在哪里打開(kāi)以及提交數(shù)據(jù)。

如果沒(méi)有設(shè)置該屬性,就會(huì)像平常一樣在本頁(yè)重定向打開(kāi)action中的url。

而如果設(shè)置為iframe的name值,即"upload"的話,就會(huì)在該iframe內(nèi)打開(kāi),因?yàn)镃SS設(shè)置為隱藏,因而不會(huì)有任何動(dòng)靜。若將display:none去掉,還會(huì)看到服務(wù)器的返回信息。

另外貼一下自己組織的類(lèi)。

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' => '請(qǐng)上傳pdf文件!',
        );
        echo json_encode($return);exit;
    }
    //自己設(shè)置的上傳文件存放路徑
    $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名稱(chēng)

    $uploadfile = $filePath .$contract_url;//上傳后的文件名地址
    //move_uploaded_file() 函數(shù)將上傳的文件移動(dòng)到新位置。若成功,則返回 true,否則返回 false。
    $result = move_uploaded_file($filetempname, $uploadfile);//假如上傳到當(dāng)前目錄下

    if($result == true){
        $orders = app::get('b2c')->model('orders')->getList('contract_no', array('order_id'=>$_POST['order_id']));  //獲取用戶(hù)發(fā)票信息
        $contracts = app::get('b2c')->model('contract_list')->getList('*', array('contract_no'=>$orders[0]['contract_no']));  //獲取用戶(hù)默認(rèn)收貨地址

        $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;
    }

}


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI