溫馨提示×

溫馨提示×

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

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

關(guān)于JS提交file 上傳文件

發(fā)布時間:2020-07-23 00:04:18 來源:網(wǎng)絡(luò) 閱讀:528 作者:熊科泉 欄目:開發(fā)技術(shù)
        最近因?qū)懸粋€上傳功能,不能刷新頁面,且頁面得有后臺返回的信息,因而不能使用form直接提交表單,嘗試使用is提交并接受返回數(shù)據(jù),不多說代碼如下:
前臺:
<form id='pdf-form' enctype="multipart/form-data" method="POST"  action="" >
    <input type="hidden" name="order_id" value="<{$order.order_id}>">
    <table align="center" width="90%" border="0">
        <tr>
            <td><b>合同信息:</b>
                <a target="_self" href="<{link app=b2c ctl=site_paycenter act=download arg0=$contract_no}>" class="active">下載</a></td>
            <{if $sign ==1}>
            <td>
                <input type="file" name="inputpdf" id="inputpdf">
                <{button id="button" onclick="upload(this);"  class="btn-import" label="合同上傳"}>
                <span id="span"  class="success notice-inline">上傳成功!</span>
            </td>
            <{/if}>
        </tr>
    </table>
</form>
js:
function upload(o){
    var data = new FormData(document.getElementById('pdf-form'));
    data.append('CustomFile','this is data');
    var PDF = new XMLHttpRequest();
    PDF.open("post",'<{link app=b2c ctl=site_paycenter act=uploadFile}>',true);
    PDF.onload = function(){
        if(PDF.status == 200){
            re =JSON.decode(PDF.responseText);
            console.log(re);
            if( re.rsp == 'succ'){
                document.getElementById('button').style.display= 'none';
                document.getElementById("span").innerHTML=re.res;
                document.getElementById('span').style.display= '';
               // document.getElementById("url").href='.'+re.url;
            }else{
                document.getElementById("span").innerHTML=re.res;
                document.getElementById('span').style.display= '';
            }
        }
    };
    PDF.send(data);
}
后臺處理以及返回信息:
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;
    }
    //自己設(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名稱

    $uploadfile = $filePath .$contract_url;//上傳后的文件名地址
    //move_uploaded_file() 函數(shù)將上傳的文件移動到新位置。若成功,則返回 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']));  //獲取用戶發(fā)票信息
        $contracts = app::get('b2c')->model('contract_list')->getList('*', array('contract_no'=>$orders[0]['contract_no']));  //獲取用戶默認(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問一下細(xì)節(jié)

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

AI