2013-04-05 97 views
1

在我的asp.net應用程序中,我使用了Textbox,buttonhidden fileupload control
按鈕時使用jQuery的單擊了我正在fileupload window如下,從文件上傳窗口讀取文件名到文本框

protected void btn_browse_Click(object sender, EventArgs e) 
{ 
    StringBuilder strScript = new StringBuilder(); 
    strScript.Append("$(document).ready(function(){"); 
    strScript.Append("$('#FileUpload1').click();"); 
    strScript.Append("});"); 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", strScript.ToString(), true); 
    txt_fileName.Text=FileUpload1.FileName; 
} 

我的問題是我無法顯示從fileuploadtextbox選定的文件名。
文件名不顯示在textbox

任何暗示。

回答

2

在服務器端,你可以這樣做:

string filename = Path.GetFileName(fID.PostedFile.FileName); 
fID.SaveAs(Server.MapPath("Files/"+filename)); 
string fpath = "Files/"+filename; 

和使用jQuery:

$(document).ready(function() { 
    $("#btnFileUpload").click(function() { 
     var FUpload = $("#FileUploadControl").val(); 
    } 
} 

對於JavaScript:

<script type="text/javascript"> 
function getFileName() { 
var varfile = document.getElementById("FileUploadControl"); 
document.getElementById("filename").value = varfile.value; 
} 
</script> 

FileUpload控件將是:

<asp:FileUpload ID="FileUploadControl" runat="server" onchange="getFileName()" 
相關問題