您的位置:首页 > 编程语言 > C#

C# 完整上传文件 代码

2010-09-20 14:50 267 查看
首先是aspx页面

<asp:FileUpload ID="FUploadAttachMent" runat="server" Height="25px" Width="665px" />
<asp:Button ID="Button1" runat="server" Text="上传"  onclick="BtnUpload_Click" />


然后是cs页面

protected void BtnUpload_Click(object sender, EventArgs e)
{
string str=this.FileUpload();
MessageBox(this.page,str);
}
#region FileUpload 上传文件(外層判斷)
protected string FileUpload(string InputData)
{

string inputPath = "";
string str3 = "";
try
{
inputPath = "//empr/Data/";
str3 = FileUploadExe(inputPath);
}
catch (Exception ex)
{
MessageBox.Show(Page, ex.Message);
}
return str3;
}

protected string FileUploadExe(string inputPath)
{
string inputTotalPath = "";
string str3 = "";
if (this.FUploadAttachMent.HasFile)
{
inputTotalPath = inputPath + this.FUploadAttachMent.FileName.ToString();
if (Files.IsDirectoryExists(inputPath))
{
this.IsExistsUp(inputTotalPath, inputPath, this.FUploadAttachMent.FileName);
//三个参数,第一个参数内容就是第二个加第三个,第二个是上传地址,第三个是上传的文件名
str3 = "文件上傳成功!";
}
else
{
Files.CreateDirectory(inputPath);//不存在则创建目录
this.IsExistsUp(inputTotalPath, inputPath, this.FUploadAttachMent.FileName);
str3 = "文件上傳成功!";
}
}
return str3;
}

#region IsExistsUp 檢測文件並上傳(裏層上傳)
protected void IsExistsUp(string InputTotalPath, string InputPath, string InputFileName)
{
try
{
if (Files.IsFileExists(InputTotalPath))
{
Files.ReNameFile(InputPath, InputFileName);//檢測源文件存在,則重命名
this.FUploadAttachMent.SaveAs(InputTotalPath);
}
else
{
this.FUploadAttachMent.SaveAs(InputTotalPath);
}
}
catch (Exception ex)
{
MessageBox.Show(Page, ex.Message);
}
}
#endregion


其中,IsExistsUp方法里面用到了Files文件里面的内容

Files.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Text;

/// <summary>
///Files 的摘要说明
/// </summary>
public class Files
{
public static string strEx = "";
public static int Start = 0;
public static int Length = 0;

public Files()
{
//
//TODO: 在此处添加构造函数逻辑
//
}

#region 成员方法

#region 目录是否存在
public static Boolean IsDirectoryExists(string InputPath)
{
Boolean strFlag = false;
try
{
if (InputPath.Length > 0)
{
strFlag = Directory.Exists(InputPath);
}
}
catch (Exception ex)
{
strEx = ex.Message;
}
return strFlag;
}
#endregion

#region 创建文件目录
public static Boolean CreateDirectory(string InputPath)
{
Boolean strFlag = false;
try
{
if (!Directory.Exists(InputPath))
{
Directory.CreateDirectory(InputPath);
strFlag = true;
}
}
catch (Exception ex)
{
strEx = ex.Message;
}
return strFlag;
}
#endregion

#region 文件是否存在
public static Boolean IsFileExists(string InputPath)
{
Boolean strFlag = false;
try
{
strFlag = File.Exists(InputPath);
}
catch (Exception ex)
{
strEx = ex.Message;
}
return strFlag;
}
#endregion

#region 重命名(替換舊文件名)
/// <summary>
/// 20100917新增代碼,為檢測文件是否存在
/// 如存在,則重命名舊文件,再上傳新文件
/// </summary>
public static void ReNameFile(string path, string filename)
{
string path1 = path + filename;
string[] filepath = Directory.GetFiles(path);
foreach (string file in filepath)
{
if (file.ToString().Trim() == path1.ToString().Trim())
{
string time = GetUploadDateTime();
string[] strArray = file.Split(new char[] { '.' });
string str1 = strArray[0].ToString();
string str2 = strArray[1].ToString();
string newfilename = str1 + time + "." + str2;
System.IO.File.Move(file, newfilename);
break;
}
}
}
#endregion

#region 删除指定文件
public static Boolean DeleteFile(string InputPath)
{
Boolean strFlag = false;
try
{
if (File.Exists(InputPath))
{
File.Delete(InputPath);
strFlag = true;
}
}
catch (Exception ex)
{
strEx = ex.Message;
}
return strFlag;
}
#endregion

#region 返回文件日期名
public static string GetUploadDateTime()
{
string strMonth = "";
string strDay = "";
string strHour = "";
string strMinute = "";
string strSecond = "";
string strResult = "";
try
{
//Month
switch (DateTime.Now.Month)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
strMonth = "0" + DateTime.Now.Month ;
break;
default:
strMonth = DateTime.Now.Month.ToString ();
break;
}
//Day
switch (DateTime.Now.Day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
strDay = "0" + DateTime.Now.Day;
break;
default:
strDay = DateTime.Now.Day.ToString();
break;
}
//Hour
switch (DateTime.Now.Hour)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
strHour = "0" + DateTime.Now.Hour;
break;
default:
strHour = DateTime.Now.Hour.ToString();
break;
}
//Minute
switch (DateTime.Now.Minute)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
strMinute = "0" + DateTime.Now.Minute;
break;
default:
strMinute = DateTime.Now.Minute.ToString();
break;
}
//Second
switch (DateTime.Now.Second)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
strSecond = "0" + DateTime.Now.Second;
break;
default:
strSecond = DateTime.Now.Second.ToString();
break;
}
strResult = DateTime.Now.Year + strMonth + strDay + strHour + strMinute + strSecond;
}
catch (Exception ex)
{
strEx = ex.Message;
}
return strResult;
}
#endregion

#region 返回文件名称
public static string GetFileName(string InputPath)
{
string strTotalPath = "";
string strResult = "";
try
{
strTotalPath = InputPath.ToUpper ().Replace(@"/", "/").ToString();
if (strTotalPath.Length > 0)
{
Start = strTotalPath.LastIndexOf("/", strTotalPath.Length) + 1;
Length = strTotalPath.Length - Start;
strResult = strTotalPath.Substring(Start, Length);
}
}
catch (Exception ex)
{
strEx = ex.Message;
}
return strResult;
}
#endregion

#endregion
}


OK,至此,我做的上传文件就成功完成了、、、、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: