上传文件出错!
请看看为什么老出错:
protected void Page_Load(object sender, EventArgs e)
{
Command1.Visible = Request.Url.ToString().IndexOf( "? ") > 0;
if (!IsPostBack & Command1.Visible)
{
Name.Text =Session[ "Name "].ToString() ;
ID_Card.Text = Session[ "ID_Card "].ToString();
Phone_Num.Text = Session[ "Phone_Num "].ToString();
Address.Text = Session[ "Address "].ToString();
Op_type.SelectedIndex = Convert.ToInt32(Session[ "Op_type "].ToString());
Remark.Text = Session[ "Remark "].ToString();
string postfile = HttpContext.Current.Request.Files[0].FileName;//这句话老出错,提示超出范围
TextBox1.Text = postfile;
}
}
其中文件上传的代码为:
<?xml version= "1.0 " encoding= 'utf-8 '?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 2.0//EN " "http://www.wapforum.org/DTD/xhtml-mobile10.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<title> 文件上传 </title>
</head>
<body>
<form id= "form1 " method= "post " action= "main.aspx?checkload=true " enctype= "multipart/form-data " >
<input type= "file " id= "file1 " />
<input type= "submit " name= "submit " value= "· 提交 · " />
</form>
</body>
</html>
------解决方案--------------------string postfile = HttpContext.Current.Request.Files[0].FileName;//这句话老出错,提示超出范围
在前面加这样的语句:
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile = files[0];
string postfile = postedFile.FileName;
------解决方案--------------------文件太大了吧?在web.config里可以设置
------解决方案--------------------你在 <form> 中加上runat=server
如果不行,试试加上runat=server后,把原来的enctype= "multipart/form-data ",这