方案一:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="SchoolWeb.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function fileUP()
{
var file = document.getElementById("<%=fileuploadImage.ClientID %>");
file.style.display="none";
file.click();
return;
}
function fileSet()
{
var file = document.getElementById("<%=fileuploadImage.ClientID %>");
if(file.value !="")
{
document.getElementById("<%=txtFile.ClientID %>").value = file.value;
}
return;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtFile" runat="server"></asp:TextBox>
<input id="btnFile" type="button" value="上传" onclick="javascript:return fileUP();" />
<asp:FileUpload ID="fileuploadImage" runat="server" Style="display:none" />
</div>
<p>
<asp:Button ID="Button1" runat="server" Text="保存" OnClick="Button1_Click" />
</p>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace SchoolWeb
{
public partial class test : System.Web.UI.Page
{
protected string FileName = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
FileName = txtFile.Text;
this.fileuploadImage.Attributes.Add("onchange", "javascript:return fileSet();");
txtFile.Attributes.Add("readonly", "readonly");
}
protected void Button1_Click(object sender, EventArgs e)
{
this.txtFile.Text = FileName;
Response.Write(FileName);
}
}
}
-------------------
方案二:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testc.aspx.cs" Inherits="SchoolWeb.testc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" language="javascript">
function UploadOnpropertychange()
{
//JS获取上传控件的值,即所上传文件的绝对路径
var varUploadFile = document.getElementByIdx("FileUpload1").value;
//将路径值通过split函数切割分成一个数组集合
//使用这个函数,这个地方主要是为了获取文件名
var varArray = varUploadFile.split('\');
//测试
// for (var i = 0;i < varArray.length;i++)
// {
// if (i == varArray.length - 1)
// {
// alert(varArray[i]);
// }
// }
//最后将所需要的文件名赋给文本框
document.getElementByIdx("txtUploadFile").value = varArray[varArray.length - 1];
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtUploadFile" runat="server"></asp:TextBox>
<input type="button" value="测试" onclick="javascript:document.getElementById('FileUpload1').click();" />
<asp:FileUpload ID="FileUpload1" runat="server" />
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace SchoolWeb
{
public partial class testc : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//先将控件隐藏
this.FileUpload1.Attributes.Add("style", "display:none");
//监视属性变化,此时主要用于捕捉文件路径的写入值
this.FileUpload1.Attributes.Add("onpropertychange", "UploadOnpropertychange()");
}
}
}
}