?? fileupload.cs
字號:
using System;
using System.IO;
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;
using System.Data.SqlClient ;
using System.Data.Common ;
using com.unicafe.common ;
using com.unicafe.security ;
using com.unicafe.ui;
namespace com.ascs.plp.common
{
public class FileTool
{
/// <summary>
/// 構造函數
/// </summary>
public FileTool()
{
}
/// <summary>
/// 上傳文件的方法
/// 參數:上傳文件空間的名字,webconfig中的上傳文件路徑的key,要上傳的文件的路徑和文件名,上傳的類型(1為不該文件名,2,該文件名)
/// 返回:字符串。如果為空,上傳失敗
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static string UploadFile(System.Web.UI.HtmlControls.HtmlInputFile FileControl ,string FileKey,string FileName,string Type)
{
try
{
if (FileName=="" || FileName==null)
{
LogService.Write ("參數不能為空。");
return "";
}
else
{
string UploadPath="";
string SaveFileName="";
UploadPath=System.Configuration.ConfigurationSettings.AppSettings[FileKey].ToString();//取當前上傳文件夾路徑
SaveFileName=GetFileNameFromPath(FileName,Type); //調用方法,取得文件名
if (SaveFileName!="")
{
FileControl.PostedFile.SaveAs(UploadPath+SaveFileName); //保存
return "../Upload/"+SaveFileName;
}
else
{
return "";
}
}
}
catch( Exception e)
{
LogService.Write (e.Message );
return "" ;
}
}
/// <summary>
/// 由路徑文件名取得文件名的方法
/// 參數:帶有路徑的文件名,上傳的類型(1為不該文件名,2,該文件名)
/// 返回:字符串。(文件名)。如果失敗,返回空
/// </summary>
/// <param name="PathFileName"></param>
/// <returns></returns>
public static string GetFileNameFromPath(string PathFileName,string Type)
{
try
{
string FileName = "";
FileName=PathFileName.Substring(PathFileName.LastIndexOf("\\")+1,PathFileName.Length - PathFileName.LastIndexOf("\\")-1);//根據路徑和文件名取文件名
if (Type=="1") //不修改文件名
{
return FileName;
}
else if (Type=="2") //調用ChangeName方法修改文件名,并返回修改后的文件名
{
return ChangeName(FileName);
}
return "";
}
catch( Exception e)
{
LogService.Write ("根據路徑名取得文件名時出錯!");
LogService.Write (e.Message );
return "";
}
}
/// <summary>
/// 使用時間修改文件名的方法。
/// 參數:要修改的文件名
/// 返回:根據當前時間修改后的文件名
/// </summary>
/// <param name="FileName"></param>
/// <returns></returns>
public static string ChangeName(string FileName)
{
try
{
string ReName= DateTime.Now.Year.ToString().Trim() +DateTime.Now.Month.ToString().Trim() +DateTime.Now.Day.ToString().Trim()
+DateTime.Now.Hour.ToString().Trim() + DateTime.Now.Minute.ToString().Trim() + DateTime.Now.Second.ToString().Trim()
+ DateTime.Now.Millisecond.ToString().Trim(); //文件名取當前的年月日小時分秒毫秒
//string ExName=FileName.Substring(FileName.Length -3,3); //取文件擴展名
Random rnd = new Random ();
ReName=ReName + ( rnd.Next(10)).ToString();
string ExName=FileName.Substring(FileName.LastIndexOf (".")+1); //取文件擴展名
return ReName +"."+ ExName;
// LogService.Write (ReName);
// LogService.Write (ExName);
}
catch(Exception e)
{
LogService.Write ("在修改文件名時出錯!");
LogService.Write (e.Message);
return "";
}
}
/// <summary>
/// 使用刪除文件的方法。
/// 參數:文件的路徑,文件名
/// 返回:刪除是否成功
/// </summary>
/// <param name="FileName"></param>
/// <returns></returns>
public static bool FileDelete(string FileKey,string FileName)
{
try
{
if (FileName=="" || FileName==null)
{
LogService.Write ("參數不能為空。");
return false;
}
else
{
string UploadPath="";
UploadPath=System.Configuration.ConfigurationSettings.AppSettings[FileKey].ToString();//取當前上傳文件夾路徑
FileName=FileName.Substring(10);
if (FileName!="")
{
File.Delete(UploadPath+FileName);
//FileControl.PostedFile.SaveAs(UploadPath+SaveFileName); //保存
return true;
}
else
{
return false;
}
}
}
catch( Exception e)
{
LogService.Write (e.Message );
return false ;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -