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,我从萤火虫返回的脚本如下:GET http://harrisdindi.com/caretech/upload/upload_patients_details/upload /files/ ,主构造函数被加载两次,即从 javascript 上传,我该如何解决这个问题?

4

1 回答 1

0

我认为您的问题是由$.ajaxFileUpload配置中的相对 url 引起的。当您url提供$.ajaxFileUpload.

您可以使用 php 中的基本 url 创建一个 javascript 变量,您可以在视图中使用类似这样的内容:

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

现在您可以在零件中使用此变量$.ajaxFileUpload

// ...
  $.ajaxFileUpload({
     url: window.BASE_URL+'/upload/upload_file/', 
// ....
于 2013-10-22T13:57:43.703 回答