关于JS提交file上传文件-创新互联

        最近因写一个上传功能,不能刷新页面,且页面得有后台返回的信息,因而不能使用form直接提交表单,尝试使用is提交并接受返回数据,不多说代码如下:
前台:

    ">
    
        
            合同信息:
                " class="active">下载
            <{if $sign ==1}>
            
                
                <{button id="button" onclick="upload(this);"  class="btn-import" label="合同上传"}>
                上传成功!
            
            <{/if}>
        
    

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;
    }
    //自己设置的上传文件存放路径
    $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;
    }

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

创新互联建站自2013年起,先为嵊泗等服务建站,嵊泗等地企业,进行企业商务咨询服务。为嵊泗企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
网页名称:关于JS提交file上传文件-创新互联
分享URL:http://lszwz.com/article/hgeog.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款