次のスクリプトがあります。
/*
* 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/ 、メイン コンストラクターが 2 回読み込まれます。つまり、javascript からアップロードされます。この問題を解決するにはどうすればよいですか?