我正在使用codeigniter和jquery。我在我的view
腳本中有一個上傳表單。上傳表單由幾個文本輸入和一個文件輸入組成。當有人點擊它的菜單鏈接時,表單會彈出(我用Jquery Modal表單完成)。是否有任何可能的方式來模擬表單提交時的ajax調用?所以當用戶點擊提交時,文件將被髮送到服務器上的一個目錄,因此信息將被存儲在數據庫中。是否可以通過iframe上傳文件以及多個數據?
這裏是我的代碼: THE VIEW:
<div id="right_column" class="grid_7 omega">
<section id="music_features">
<header>
<hgroup>
<h1>Your Playlist</h1>
<p>Up to only 3 files allowed for beta version</p>
</hgroup>
</header>
<p id="song_upload_menu"><?php echo anchor('#', 'Upload Song');?></p>
<article id="song_upload_form">
<div id="song_upload_info">
<?php echo $this->session->flashdata('info'); ?>
</div>
<?php echo form_open_multipart('profile/do_upload_song');?>
<input type="hidden" id="user_id" name="user_id" value="<?php echo $user->id_str;?>" />
<label for="title">Song Title</label>
<input type="text" name="title" id="title" size="30" value="<?php echo set_value('title'); ?>"/><br />
<label for="album"> Album Title </label>
<input type="text" name="album" id="album" size="30" value="<?php echo set_value('album'); ?>"/><br />
<label for="artist">Artist</label>
<input type="text" name="artist" id="artist" size="20" value="<?php echo set_value('artist'); ?>"/><br />
<input type="file" name="userfile" size="20" /><br />
<input type="radio" name="license" id="license" value="owner"/>I AM the artist/owner of this song and I have the right for distribution<br />
<input type="radio" name="license" id="license" value="not owner"/>I AM NOT the artist/owner of this song BUT I have the right for distribution</br>
<input type="checkbox" name="song_license_agreement"/>By selecting this option, I swear that all information entered is right<br />
<input type="submit" id="song_upload_submit" value="Upload Your Song"/>
</form>
</article>
<ul id="playlist">
<!-- your song goes here -->
</ul>
</section>
這是我的一個功能來處理TE上傳:
function do_upload_song()
{
//$file_element_name = 'users_song';
$config['upload_path'] = './media/';
$config['allowed_types'] = 'mp3';
$config['max_size'] = '30720';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
//set validation rules
$this->form_validation->set_rules('title', 'Song title', 'trim|required|xss_clean');
$this->form_validation->set_rules('album', 'Album', 'trim|required|xss_clean');
$this->form_validation->set_rules('artist', 'Artist', 'required|xss_clean');
$this->form_validation->set_rules('license', 'License', 'required');
$this->form_validation->set_rules('song_license_agreement', 'License Agreement', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('info', validation_errors());
redirect('profile');
}
else
{
if (! $this->upload->do_upload())
{
$this->session->set_flashdata('info', $this->upload->display_errors());
redirect('profile');
}
else
{
$data = $this->upload->data();
$add_song = $this->media_model->add_song($data['file_name']);
if($add_song)
{
$this->session->set_flashdata('info', 'Your song has been uploaded');
redirect('profile');
}
else
{
unlink($data['full_path']);
$this->session->set_flashdata('info', 'Song upload fail');
redirect('profile');
}
}
@unlink($_FILES);
}
}
,這是我的.js
$(document).ready(function(){
$('#song_upload_form').hide();
$('#song_upload_menu').click(function(e){
$('#song_upload_form').dialog({
title: 'Upload the cool track man!',
width: 520,
show: 'fade',
hide: 'fade',
modal: true
});
e.preventDefault();
});
});
JS腳本使模態窗體彈出,但我不知道該怎麼做。我只能通過ajax發送數據。但文件是不同的。我知道他們不能通過Ajax發送。我在網上發現的許多文章告訴我關於Iframe的問題,但他們只處理文件。我無法找到一個切實可行的解決方案來解決我的問題,即模擬ajax同時發送數據和文件。任何建議傢伙?謝謝
如果我有幾個輸入類型,如文本或單選按鈕(就像上面的代碼),它仍然可以使用IFrame。我試過最後一次,但它似乎不起作用。或者我只是錯過了一些東西。我會嘗試你給我的教程。我會讓你知道:-) – under5hell
我相信它應該仍然有效。這是接下來我要做的事情,但我不願意開始:) – mowwwalker
@ under5hell,我做了一些測試,並且沒有爲我工作的是,我沒有設置enctype按照PHP文檔的規定:'enctype =「multipart/form-data」'http://us.php.net/manual/en/features.file-upload.post-method.php – mowwwalker