代码如下using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace 第四章
{
/// <summary>
/// Upload 的摘要说明。
/// </summary>
public class Upload : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile Uploader;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
if (Uploader.PostedFile != null)
{
try
{
// retrieve the destination dir path
string destDir = Server.MapPath("./Upload");
// extract the filename part from the full path of the posted file
string fileName = System.IO.Path.GetFileName(Uploader.PostedFile.FileName);
// combine the destination directory with the filename
string destPath = System.IO.Path.Combine(destDir, fileName);
// save the file on the server
Uploader.PostedFile.SaveAs(destPath);
Response.Write("Thanks for submitting your file");
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
}
} }
}
原因是什么?书上的另一个HTML跟CS一起的源码没问题,如下:<form runat="server" enctype="multipart/form-data">
Select a file to upload:<br>
<input type="file" runat="server" ID="Uploader">
<input type="button" runat="server" ID="Upload" value="Upload" OnServerClick="Upload_Click">
</form><script runat="server" language="C#">
void Upload_Click(object sender, EventArgs e)
{
if (Uploader.PostedFile != null)
{
try {
// retrieve the destination dir path
string destDir = Server.MapPath("./Upload");
// extract the filename part from the full path of the posted file
string fileName = System.IO.Path.GetFileName(Uploader.PostedFile.FileName);
// combine the destination directory with the filename
string destPath = System.IO.Path.Combine(destDir, fileName);
// save the file on the server
Uploader.PostedFile.SaveAs(destPath);
Response.Write("Thanks for submitting your file");
}
catch (Exception exc) {
Response.Write(exc.Message);
}
}
}
</script>
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace 第四章
{
/// <summary>
/// Upload 的摘要说明。
/// </summary>
public class Upload : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile Uploader;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
if (Uploader.PostedFile != null)
{
try
{
// retrieve the destination dir path
string destDir = Server.MapPath("./Upload");
// extract the filename part from the full path of the posted file
string fileName = System.IO.Path.GetFileName(Uploader.PostedFile.FileName);
// combine the destination directory with the filename
string destPath = System.IO.Path.Combine(destDir, fileName);
// save the file on the server
Uploader.PostedFile.SaveAs(destPath);
Response.Write("Thanks for submitting your file");
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
}
} }
}
原因是什么?书上的另一个HTML跟CS一起的源码没问题,如下:<form runat="server" enctype="multipart/form-data">
Select a file to upload:<br>
<input type="file" runat="server" ID="Uploader">
<input type="button" runat="server" ID="Upload" value="Upload" OnServerClick="Upload_Click">
</form><script runat="server" language="C#">
void Upload_Click(object sender, EventArgs e)
{
if (Uploader.PostedFile != null)
{
try {
// retrieve the destination dir path
string destDir = Server.MapPath("./Upload");
// extract the filename part from the full path of the posted file
string fileName = System.IO.Path.GetFileName(Uploader.PostedFile.FileName);
// combine the destination directory with the filename
string destPath = System.IO.Path.Combine(destDir, fileName);
// save the file on the server
Uploader.PostedFile.SaveAs(destPath);
Response.Write("Thanks for submitting your file");
}
catch (Exception exc) {
Response.Write(exc.Message);
}
}
}
</script>
好象dotnet的软件都回填加一些东东!有时就是不能用!
---------------
那就是路径有问题,你可用如下代码将你的路径打印出来看看是不是对的
this.Response.Write(你的路径)
this.Response.End()
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Upload</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form enctype="multipart/form-data" runat="server">
<INPUT id="Uploader" type="file" runat="server"> <INPUT id="go" type="button" value="Button" runat="server">
</form>
</body>
</HTML>
_______________________Upload.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace 第四章
{
/// <summary>
/// Upload 的摘要说明。
/// </summary>
public class Upload : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton go;
protected System.Web.UI.HtmlControls.HtmlInputFile Uploader;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.go.ServerClick += new System.EventHandler(this.go_ServerClick);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
private void go_ServerClick(object sender, System.EventArgs e)
{
try
{
// retrieve the destination dir path
string destDir = Server.MapPath("./Upload");
// extract the filename part from the full path of the posted file
string fileName = System.IO.Path.GetFileName(Uploader.PostedFile.FileName);
// combine the destination directory with the filename
string destPath = System.IO.Path.Combine(destDir, fileName);
// save the file on the server
Uploader.PostedFile.SaveAs(destPath);
Response.Write("Thanks for submitting your file");
Response.Write("aa:"+destDir+"bb:"+fileName);
}
catch (Exception exc)
{
Response.Write(exc.Message);
string destDir = Server.MapPath("./Upload");
string fileName = System.IO.Path.GetFileName(Uploader.PostedFile.FileName);
Response.Write("aa:"+destDir+"bb:"+fileName);
}
} }
}
try
检查 UPLOAD目录是否存在,NTFS格式的话,给UPLOAD目录添加ASPNET权限,或者直接添加everyone完全控制权限如果硬盘上还有FAT32格式的盘,可以把保存路径设置到FAT32的盘试试
,一个是书上不分离的,书上的那个确实没问题!