<input type="file" ID="UploadedFile" runat="server" size="35" NAME="UploadedFile">
.....
protected void Upload_Click(object sender, EventArgs e)
{
// proceed only if a file has been specified
if (UploadedFile.PostedFile != null && UploadedFile.PostedFile.FileName.Length > 0 )
{
string destDir = Server.MapPath(folderPath); try
{
// save the to the current directory
string fileName = Path.GetFileName(UploadedFile.PostedFile.FileName);
UploadedFile.PostedFile.SaveAs(Path.Combine(destDir, fileName));
// refresh the page
Response.Redirect("BrowseFiles.aspx?Folder=" + folderPath);
}
catch (Exception exc)
{
StatusMessage.Text = exc.Message;
StatusMessage.Visible = true;
}
}
}
.....
protected void Upload_Click(object sender, EventArgs e)
{
// proceed only if a file has been specified
if (UploadedFile.PostedFile != null && UploadedFile.PostedFile.FileName.Length > 0 )
{
string destDir = Server.MapPath(folderPath); try
{
// save the to the current directory
string fileName = Path.GetFileName(UploadedFile.PostedFile.FileName);
UploadedFile.PostedFile.SaveAs(Path.Combine(destDir, fileName));
// refresh the page
Response.Redirect("BrowseFiles.aspx?Folder=" + folderPath);
}
catch (Exception exc)
{
StatusMessage.Text = exc.Message;
StatusMessage.Visible = true;
}
}
}
给你一段上传文件的代码:
private void Button1_Click(object sender, System.EventArgs e)
{
if(this.myFile.PostedFile!=null) //upload textbox
{
//接收路径
string strPath=Request.QueryString["path"]; //current direction //如果接收的路径为空,则获取当前路径
if(strPath==null)
{
strPath=Request.ServerVariables["APPL_PHYSICAL_PATH"];
}
else
{
if(strPath.Substring(strPath.Length-1,1)!="\\")
{
strPath=strPath+"\\";
}
} //处理字符串
string strName=this.myFile.PostedFile.FileName;
int i=strName.LastIndexOf("\\");
string strNewName=strName.Substring(i);
string strFullName=strPath+strNewName;
//保存文件
this.myFile.PostedFile.SaveAs(strFullName);
//Response.Redirect("index.aspx?path="+strPath);
this.Label2.Text="<center><font color=red>上传成功</font></center>";
ListFile();
}
}
http://support.microsoft.com/default.aspx?scid=kb;EN-US;323246