다중 업로드 파일 클래스를 맹글어 보자
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
/// <summary>
/// UploadContentsManage의 요약 설명입니다.
/// </summary>
///
namespace ddarang.Upload
{
public class UploadContentsManage
{
//업로드 경로 web.config 가져오기
string ROOT_PATH = ConfigurationManager.AppSettings["uploadpath"];
string SAVE_PATH;
/// <summary>
/// 업로드 상세 경로 설정
/// </summary>
/// <param name="path"></param>
public UploadContentsManage(UploadPath path)
{
switch (path)
{
case UploadPath.bbsimg:
SAVE_PATH = ROOT_PATH + "/";// "/bbsImg/";
break;
case UploadPath.bbsattach:
SAVE_PATH = ROOT_PATH + "/bbsAttach/";
break;
default:
SAVE_PATH = ROOT_PATH + "/";
break;
}
}
/// <summary>
/// 다중 업로드 파일이름 저장
/// </summary>
/// <param name="uploadFiles"></param>
/// <returns></returns>
public Dictionary<string, string> FileSave(HttpFileCollection uploadFiles)
{
Dictionary<string, string> savedFiles = new Dictionary<string, string>();
for (int i = 0; i < uploadFiles.Count; i++)
{
HttpPostedFile uf = uploadFiles[i];
if (uf.ContentLength > 0)
{
string savedFileName = string.Empty;
try
{
//경로를 포함한 파일 이름
string strFileFullName = uf.FileName;
//파일의 이름을 구함
string m_FileName = strFileFullName.Substring(strFileFullName.LastIndexOf("\\") + 1);
//확장자
string fext = strFileFullName.Substring(strFileFullName.LastIndexOf(@".") + 1).ToLower();
//파일명 날짜로 변경
savedFileName = uploadFiles.Keys[i].Substring(uploadFiles.Keys[i].LastIndexOf(@"$")+1) + DateTime.Now.Ticks.ToString() + "." + fext;
uf.SaveAs(SAVE_PATH + savedFileName);
savedFiles.Add(uploadFiles.Keys[i].Substring(uploadFiles.Keys[i].LastIndexOf(@"$") + 1), savedFileName);
}
catch (Exception ex)
{
throw new Exception("파일 저장 에러 - " + savedFileName, ex);
}
}
}
return savedFiles;
}
/// <summary>
/// 업로드 파일 삭제
/// </summary>
/// <param name="path"></param>
/// <param name="removeFiles"></param>
public static void FileRemove(UploadPath path, ArrayList removeFiles)
{
string root_path = ConfigurationManager.AppSettings["uploadpath"];
string save_path = string.Empty;
switch (path)
{
case UploadPath.bbsimg:
save_path = root_path + "/bbsImg/";
break;
case UploadPath.bbsattach:
save_path = root_path + "/bbsAttach/";
break;
default:
save_path = root_path + "/";
break;
}
for (int i = 0; i < removeFiles.Count; i++)
{
FileInfo file = new FileInfo(string.Concat(save_path, removeFiles[i].ToString()));
if (file.Exists)
{
try
{
file.Delete();
}
catch (Exception ex)
{
throw new Exception("파일 삭제 에러 - " + file.FullName, ex);
}
}
}
}
/// <summary>
/// 파일을 다운로드한다.
/// </summary>
/// <param name="page"></param>
/// <param name="gubun"></param>
/// <param name="serverSavedFileName"></param>
public static string DownloadFilePath(System.Web.UI.Page page, UploadPath path, string serverSavedFileName)
{
string root_path = ConfigurationManager.AppSettings["uploadUrl"];
string save_path = string.Empty;
switch (path)
{
case UploadPath.bbsimg:
save_path = root_path + "/bbsImg/";
break;
case UploadPath.bbsattach:
save_path = root_path + "/bbsAttach/";
break;
default:
save_path = root_path + "/";
break;
}
return string.Concat(save_path, serverSavedFileName).Replace("\\", "/");
}
}
/// <summary>
/// 업로드 경로 구분
/// </summary>
public enum UploadPath
{
bbsimg,
bbsattach
}
}
===파일을 업로드 할때===================================================
private Dictionary<string, string> Upload()
{
UploadContentsManage ucm = new UploadContentsManage(UploadPath.bbsimg);
Dictionary<string, string> savedFiles = ucm.FileSave(Request.Files);
return savedFiles;
}
//저장 버튼 클릭
protected void btnAdd_Click(object sender, EventArgs e)
{
Dictionary<string, string> attfiles = Upload();
string fname = attfiles["photo"].ToString();
Response.Write("업로드된 파일명 : "+ fname);
}