2013-10-22 28 views
0

我有以下腳本:網址嵌入式兩次

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
$(document).ready(function() { 
$(function() { 
    $('#upload_file').submit(function(e) { 
     e.preventDefault(); 
     $.ajaxFileUpload({ 
     url   :'./upload/upload_file/', 
     secureuri  :false, 
     fileElementId :'userfile', 
     dataType : 'json', 
     data  : { 
      'title'   : $('#title').val() 
     }, 
     success : function (data, status) 
     { 
      if(data.status != 'error') 
      { 
       $('#files').html('<p>Reloading files...</p>'); 
       refresh_files(); 
       $('#title').val(''); 
      } 
      alert(data.msg); 
     } 
     }); 
     return false; 
    }); 
}); 
    setInterval(function refresh_files() 
{ 
    $.get('./upload/files/') 
    .success(function (data){ 
     $('#files').html(data); 
    }); 
},5000 
); 

$('.delete_file_link').live('click', function(e) { 
    e.preventDefault(); 
    if (confirm('Are you sure you want to delete this file?')) 
    { 
     var link = $(this); 
     $.ajax({ 
     url   : '<?php echo base_url()?>/upload/delete_file/' + link.data('file_id'), 
     dataType : 'json', 
     success  : function (data) 
     { 
      files = $('#files'); 
      if (data.status === "success") 
      { 
       link.parents('li').fadeOut('fast', function() { 
        $(this).remove(); 
        if (files.find('li').length == 0) 
        { 
        files.html('<p>No Files Uploaded</p>'); 
        } 
       }); 
      } 
      else 
      { 
       alert(data.msg); 
      } 
     } 
     }); 
    } 
}); 
}); 

而且在我的控制器以下功能:

public function upload_patients_details(){ 
     $id=$this->uri->segment(3); 
     $sql = "SELECT CONCAT(fname, '', lname) AS Patients_Name FROM patients 
WHERE id = '$id' LIMIT 0 , 1"; 
     $result = $this->db->query($sql); 
     $result1 = $result->result_array(); 
     foreach ($result1 as $key) { 

      $patients_name = $key['Patients_Name']; 

      $data['Patients_Name']=$patients_name; 

     $data['dropdown_type']=$this->get_radiology_type(); 

     $this->load->view('upload',$data); 
     } 

    } 
public function upload_file() 
{ 
    $status = ""; 
    $msg = ""; 
    $file_element_name = 'userfile'; 

    if (empty($_POST['title'])) 
    { 
     $status = "error"; 
     $msg = "Please enter a title"; 
    } 

    if ($status != "error") 
    { 
     $config['upload_path'] = './radiology/'; 
     $config['allowed_types'] = 'gif|jpg|png|doc|txt'; 
     $config['max_size'] = 1024 * 8; 
     $config['encrypt_name'] = TRUE; 

     $this->load->library('upload', $config); 

     if (!$this->upload->do_upload($file_element_name)) 
     { 
     $status = 'error'; 
     $msg = $this->upload->display_errors('', ''); 
     } 
     else 
     { 
     $data = $this->upload->data(); 
     $file_id = $this->files_model->insert_file($data['file_name'], $_POST['title']); 
     if($file_id) 
     { 
      $status = "success"; 
      $msg = "File successfully uploaded"; 
     } 
     else 
     { 
      unlink($data['full_path']); 
      $status = "error"; 
      $msg = "Something went wrong when saving the file, please try again."; 
     } 
     } 
     @unlink($_FILES[$file_element_name]); 
    } 
    echo json_encode(array('status' => $status, 'msg' => $msg)); 
} 
public function files() 
{ 
    $files = $this->files_model->get_files(); 
    $this->load->view('files', array('files' => $files)); 
} 
public function delete_file($file_id) 
{ 
    if ($this->files_model->delete_file($file_id)) 
    { 
     $status = 'success'; 
     $msg = 'File successfully deleted'; 
    } 
    else 
    { 
     $status = 'error'; 
     $msg = 'Something went wrong when deleteing the file, please try again'; 
    } 
    echo json_encode(array('status' => $status, 'msg' => $msg)); 
} 

而以下幾種觀點:

<!doctype html> 
<html> 
<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
    <script src="<?php echo base_url()?>js/site.js"></script> 
    <script src="<?php echo base_url()?>js/ajaxfileupload.js"></script> 
    <link href="<?php echo base_url()?>css/upload.css" rel="stylesheet" /> 
</head> 
<body> 
    <h1>Upload File</h1> 
    <form method="post" action="" id="upload_file"> 

     <?php //foreach($Patients_Name as $scan_types){?> 
     <label for="title">Patient Name:</label> 
     <input type="text" name="title" id="title" readonly value="<?php echo $Patients_Name?>" /> 


     <label for="userfile">File</label> 
     <input type="file" name="userfile" id="userfile" size="20" /> 

     <label> Comment/Description:</label> 
     <textarea rows="4" cols="20" id="comments" name="comments" placeholder="Please provide a brief description"> </textarea> 


     <td> <p> 
     <label for="scan_type">Scan Type <span class="required">*</span></label> 
     <?php echo form_error('scan_type'); ?> 




     <select data-placeholder="Select a Scan Type..." class="scan_type" name="scan_type" id="scan_type" > 
       <option ></option>       
            <?php foreach($dropdown_type as $scan_types){?> 
           <option value="<?php echo $scan_types['dropdown_type']?>" id="<?php echo $scan_types['dropdown_type'] ?>" ><?php echo $scan_types['dropdown_type']?></option> 
           <?php } ?></select> 

</p> 
       </td> 
     <input type="submit" name="submit" id="submit" /> 
    </form> 
    <h2>Files</h2> 
    <div id="files"></div> 
</body> 
</html 

當我運行腳本:http://harrisdindi.com/caretech/upload/upload_patients_details/40,我從firebug取回的腳本如下:GET http://harrisdindi.com/caretech/upload/upload_patients_details/upload/files/,主要的con結構正在加載兩次,即從JavaScript上傳,我該如何解決這個問題?

回答

0

我認爲你的問題是由$.ajaxFileUpload配置中的相對URL引起的。 當您爲$.ajaxFileUpload提供url密鑰時,請嘗試在您的javascript代碼中使用絕對網址。

你可以做創建從PHP基礎URL與您的看法是這樣的一個JavaScript變量:

// ... 
    $.ajaxFileUpload({ 
    url: window.BASE_URL+'/upload/upload_file/', 
// .... 

// in <head> or somewhere near the top before any other <script> tag 
<script> 
    var BASE_URL = '<?php print base_url(); ?>'; 
</script> 

現在你可以在$.ajaxFileUpload部分使用這個變量