2017-07-24 159 views
1

我是OpenCart的初学者,我试图使用PHP在服务器上传文件。我已经完成了下面的代码。我的问题是,文件在服务器上成功上传,但如果有一些错误,我试图加载LoadA()函数,但结果没有显示在网站上,而是显示在console.log我不知道错误是什么。OpenCart未在页面加载内容?

控制器/ boxupload/upload.php的

public function add() 
{ 

    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) 
    { 
     if(success) //files are succesfully uploading 
     { 
      //doing stuff 
     } 
     if (it is fail) 
     { 

      $this->LoadA(); // this is not loading in site. Instead it is showing in console.log 
     } 
    } 
} 

public function LoadA() 
{ 
    $data['token'] = $this->session->data['token']; 
    $this->document->setTitle("!!!!!!!!!!!!Upload failed!!!!!!!!"); 
    $data['add'] = $this->url->link('boxupload/upload/add', 'token=' . $this->session->data['token'], 'SSL'); 
    $data['header'] = $this->load->controller('common/header'); 
    $data['column_left'] = $this->load->controller('common/column_left'); 
    $data['footer'] = $this->load->controller('common/footer'); 
    $this->response->setOutput($this->load->view('boxupload/upload.tpl', $data)); 
} 

然后这一个

控制器TPL文件/ boxupload/upload.tpl

$('#button-upload').on('click', function() { 
    $('#progress-bar').css('width', '0%'); 
    $('#form-upload').remove(); 

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>'); 

    $('#form-upload input[name=\'file\']').trigger('click'); 

    if (typeof timer != 'undefined') { 
     clearInterval(timer); 
    } 
    var WID = 30; 
    timer = setInterval(function() { 
     if ($('#form-upload input[name=\'file\']').val() != '') { 
      clearInterval(timer); 

      // Reset everything 
      $('.alert').remove(); 
      $('#progress-bar').css('width', '0%'); 
      $('#progress-bar').removeClass('progress-bar-danger progress-bar-success'); 
      $('#progress-text').html(''); 

      $.ajax({ 
       url: '<?php echo "index.php?route=boxupload/upload/add&token=$token"; ?>', 

       type: 'post', 
       //dataType: 'json', 
       data: new FormData($('#form-upload')[0]), 
       cache: false, 
       contentType: false, 
       processData: false, 
       beforeSend: function() { 
        $('#button-upload').button('loading'); 

        $('#progress-bar').css('width', '50%'); 

       }, 
       complete: function() { 
        $('#button-upload').button('reset'); 
        $('#progress-bar').css('width', '100%'); 

       }, 
       success: function() 
       { $('#progress-bar').css('width', '80%'); 


       }, 

      }); 
     } 

    }, 500); 
}); 

Please see the bellow image 请参阅图像中突出显示的区域。

+0

您需要调用$ this-> LoadA();而不是$ this-> load(); ? –

+0

也如果我没有错,你需要设置成功或错误,如下这样的HTML: 成功:功能(html){ \t \t $('#div-name')。html(html); }, ,因为你有html的回复 –

回答

1

试试这个:

dataType: 'html', 

success: function(html) 
{ 
    $('#progress-bar').css('width', '80%'); 
    $('#your-div').html(html); 

}, 
+0

这是行得通的。并请解释为什么同一控制器中的功能不起作用。 – mkHun

+0

因为你正在返回视图的响应,但没有设置它在用户界面上的任何地方..我不明白你的意思是在同一控制器功能不工作.. –

1

你.tpl文件应该是在一个视图。

$this->response->setOutput($this->load->view('boxupload/upload.tpl', $data)); 

加载文件

view/theme/default/template/boxupload/upload.tpl 

而且,看一些其他的模板文件。他们通常主要是HTML。

此外,你将要重构你的名字,以便他们使用正常的OpenCart命名约定。

controller/extension/boxupload/upload.php 
view/theme/default/template/extension/boxupload/upload.tpl 
etc. 
+0

谢谢你的回应。但它不起作用。在服务器上找不到像'tpl文件一样的错误。我已经通过重定向来修复它。 – mkHun