从文件上传窗口读取文件名到文本框
问题描述:
在我的asp.net应用程序中,我使用了Textbox
,button
和hidden 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;
}
我的问题是我无法显示从fileupload
到textbox
选定的文件名。
文件名不显示在textbox
任何暗示。
答
在服务器端,你可以这样做:
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()"