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();
}
}
}
这里没有给出数据库的写入,其实只要把mPath,mFileName的值写入数据库即可。
<%@ 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的值写入数据库即可。
相关文章推荐
- C#上传文件到网站指定目录
- C# 上传文件到指定目录,单击下载文件,删除指定目录文件,打开文件列表
- 用C#列出指定目录下的所有指定扩展名(多个扩展名)的文件
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表...
- 利用Windows Service,定时监控某一目录,发现新文件后自动上传到指定web服务器
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等 收藏
- C#.NET中遍历指定目录下的文件(及所有子目录及子目录里更深层目录里的文件)
- C# 将引用的DLL文件放到指定的目录下
- 将前端页面传来的单个文件上传到系统目录指定目录下
- 解读分开存储网站程序文件和上传目录文件的方法_动易Cms教程
- c#winform选择文件,文件夹,打开指定目录方法
- c#winform选择文件,文件夹,打开指定目录方法
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- C# 遍历指定目录下的所有文件及文件夹以及遍历数据库的方法
- net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- 文件上传简介1---上传到指定的目录
- C#获取项目指定目录下文件的方法
- C# 删除指定目录下的 所有指定文件、文件夹
- c# 指定目录批量压缩文件
- 转载:C# 将引用的DLL文件放到指定的目录下