2011-07-21 21 views

回答

33

假設您使用表格:

// select the file input (using a id would be faster) 
$('input[type=file]').change(function() { 
    // select the form and submit 
    $('form').submit(); 
}); 

編輯:爲了保持這個答案了最新:

有通過AJAX上傳文件,而這裏介紹黑客的好辦法: http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/

+0

發送數據到數據庫必須使用ajax調用嗎?請解釋更多... –

+0

@Mehd:這不是ajax。當您更改文件時,表單將自動開始發送 – genesis

+0

@Mehd:查看完整上傳解決方案的成因答案。 – binarious

5

使用jQuery的插件uploadify

這是偉大的插件,它有許多選項,並自動上傳,太

$(document).ready(function() { 
    $('#file_upload').uploadify({ 
    'uploader' : '/uploadify/uploadify.swf', 
    'script' : '/uploadify/uploadify.php', 
    'cancelImg' : '/uploadify/cancel.png', 
    'folder' : '/uploads', 
    'auto'  : true 
    }); 
}); 
+3

我不使用插件 –

+0

如果您想要使用html上傳器,則非商業項目花費5美元,商業項目花費100美元。 – Adam

3

剛纔設置的自動爲true ..'auto':true

+2

你可以添加一個鏈接,你看到這個工作,因爲我找不到像這樣的東西。 –