2015-05-05 54 views
-1

是否可以在點擊按鈕時調用fileupload事件?如何在不使用html控件的情況下調用html控件事件

我需要的是這樣的

<input type="button" onclick="return buttonClicked()"> 

和調用文件上傳事件應該進來buttonClicked()事件

function buttonClicked() 
    { 
     preventDefault(); 
     ...File upload event 
    } 

我一直在尋找這個功能了一段時間,它似乎很簡單功能,但無法找到任何解決方案。

真正需要幫助的就這一個

+0

你只是試圖觸發一個事件,或者你想實際上傳一個文件? – Mathletics

+0

@Mathletics觸發事件是第一步。當然,我需要上傳文件 – Alex

回答

0

你可以用以下的jQuery插件:

jQuery的站點鏈接: https://plugins.jquery.com/uploadfile/

Github上鍊接: https://github.com/hayageek/jquery-upload-file

的pluging公開下列事件回撥:

$("#aDivforUpload").uploadFile({ 
 
    url: "http://hayageek.com/examples/jquery/ajax-multiple-file-upload/upload.php", 
 
    multiple: true, 
 
    fileName: "myfile", 
 
    onSubmit: function(files) { 
 
     $("#eventsmessage").html($("#eventsmessage").html() + "<br/>Submitting:" + JSON.stringify(files)); 
 
    }, 
 
    onSuccess: function(files, data, xhr) { 
 
     $("#eventsmessage").html($("#eventsmessage").html() + "<br/>Success for: " + JSON.stringify(data)); 
 
    }, 
 
    afterUploadAll: function() { 
 
     $("#eventsmessage").html($("#eventsmessage").html() + "<br/>All files are uploaded"); 
 

 
    }, 
 
    onError: function(files, status, errMsg) { 
 
     $("#eventsmessage").html($("#eventsmessage").html() + "<br/>Error for: " + JSON.stringify(files)); 
 
    } 
 
});

這裏是不錯的文檔演示了這個插件:

http://hayageek.com/docs/jquery-upload-file.php

UPDATE: 這裏是的jsfiddle我已經創建了:http://jsfiddle.net/raishul/2x32tq3p/

+0

這是文件將被上傳的網址嗎?:http://hayageek.com/examples/jquery/ajax-multiple-file-upload/upload.php – Alex

+0

什麼服務器端您正在使用的語言?你可以將它指向你的文件處理服務器端頁面。檢查更新。 – Raishul

+0

我正在使用asp.net – Alex

0

如果您正在使用asp.net,您可以使用通用處理程序並通過ajax調用它來上傳文件。

如果您對此感興趣,請讓我知道給您一個完整的答案。

在asp.net 你可以使用this Tutorial其中幾乎我所做的。這將允許您上傳文件而無需發回。還有另一種使用asp.net控件FileUpload在服務器端上傳的方式,但我不推薦它。

+0

確定給我完整的答案 – Alex

+0

@Alex答案更新爲鏈接。 – Baso

相關問題