您的位置:首页 > 运维架构 > 网站架构

C#上传文件到网站指定目录

2009-03-11 13:06 471 查看
页面代码--

<%@ 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" enctype="multipart/form-data">

<div>

<input id="fileUp" name="File1" type="file" runat="server" />

<asp:Button ID="btnUpFile" runat="server" Text="上传" OnClick="btnUpFile_Click" />

<asp:Label ID="strState" runat="server" />

<img src="upfile/2.jpg" />

</form>

</body>

</html>

cs代码--

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;

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 = "",fileExtName = "",mFileName,mPath;

System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传信息<hr color=red>");

if(fileUp.PostedFile.FileName != "")

{

//取得文件路径

filepath = fileUp.PostedFile.FileName;

//取得文件扩展名

fileExtName = filepath.Substring(filepath.LastIndexOf(".") + 1);

//取得服务器上的相对路径

<FONT color=#ff0000>mPath = Server.MapPath("upfile/");

</FONT>

//取得文件名

<FONT color=#ff0000>mFileName = filepath.Substring(filepath.LastIndexOf("\\") + 1);

</FONT>

//取得上传文件的信息

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();

}

}

}

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;
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 = "",fileExtName = "",mFileName,mPath;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传信息<hr color=red>");
if(fileUp.PostedFile.FileName != "")
{
//取得文件路径
filepath = fileUp.PostedFile.FileName;
//取得文件扩展名
fileExtName = filepath.Substring(filepath.LastIndexOf(".") + 1);
//取得服务器上的相对路径
mPath = Server.MapPath("upfile/");

//取得文件名
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();
}
}
}

这里没有给出数据库的写入,其实只要把mPath,mFileName的值写入数据库即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: