书上的小实例,抄写代码,运行出现问题。
页面代码:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<input id="fileUp" type="file" name="File1" runat="server" />
<asp:Button ID="btnUpFile" runat="server" Text="上传" OnClick="btnUpFile_Click" /><br />
<asp:Label ID="strState" runat="server"></asp:Label>
<!--<asp:FileUpload ID="FileUpload1" runat="server" />-->
</form>
</body>
</html>
注:<input type="file" runat="server"/>应该和控件<asp:FileUpload/>是一样的效果。
页面处理代码:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpFile_Click(object sender, EventArgs e)
{
string filePath = "";
string fileExtName = "";
string mFileName;
string mPath;
StringBuilder strMsg = new StringBuilder("上传的文件信息:<hr color=red>");
if ("" != fileUp.PostedFile.FileName)
{
//获取文件路径
filePath = fileUp.PostedFile.FileName;
//获取文件扩展名
fileExtName = filePath.Substring(filePath.LastIndexOf(".") + 1);
try
{
//取得与Web服务器上指定的虚拟路径相对应的物理文件路径
mPath = Server.MapPath("upedfile/");
//取得文件名
mFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
//取得上传文件的信息
strMsg.Append("上传的文件类型:" + fileUp.PostedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件路径:" + fileUp.PostedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + mFileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtName);
//保存上传文件到指定的目录
fileUp.PostedFile.SaveAs(mPath + mFileName);
strState.Text = strMsg.ToString();
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
}
}
}
运行,页面出错:
跟踪变量,发现所有的地址都多个斜杠,这是为什么呢?