업로드 - 내장 객체 사용
public static string[] File_UpLoad(FileUpload FileUpload1, string strBasePath, string sFileName)
{
string[] strReturnFile = new string[2];
if (FileUpload1.HasFile)
{
DirectoryInfo dirInfo = new DirectoryInfo(strBasePath);
if (!dirInfo.Exists) // 디렉토리가 존재하지 않을 경우 생성
{
dirInfo.Create();
}
// 순수 파일명
string strCurrFileNM = Path.GetFileName(FileUpload1.PostedFile.FileName);
// 전체 파일명(경로 포함)
string FullFileNM = strBasePath + strCurrFileNM;
FileInfo fileInfos = new FileInfo(FullFileNM);
string strFullFileNM = strBasePath + strCurrFileNM;
if (sFileName != "")
{
strFullFileNM = strBasePath + sFileName + fileInfos.Extension;
}
FileInfo fileInfo = new FileInfo(strFullFileNM);
strReturnFile[1] = fileInfo.Extension.Substring(1);
// 파일이 존재한대면
if (fileInfo.Exists)
{
int fileCount = 0;
string strNewFileNM = string.Empty;
// 확장자
string strFileExtension = fileInfo.Extension;
// 확장자를 제외한 파일명
string strRealFileNM = fileInfo.Name.Replace(strFileExtension, "");
if (sFileName != "")
{
strRealFileNM = sFileName;
}
do
{
fileCount++;
// 파일명 재설정 : 파일명(num).jpg
strNewFileNM = strRealFileNM + "(" + fileCount + ")" + strFileExtension;
fileInfo = new FileInfo(strBasePath + strNewFileNM);
}
while (fileInfo.Exists);
}
else
{
string strFileExtension = fileInfo.Extension;
// 확장자를 제외한 파일명
string strRealFileNM = fileInfo.Name.Replace(strFileExtension, "");
if (sFileName != "")
{
strRealFileNM = sFileName + strFileExtension;
}
else
{
strRealFileNM = strRealFileNM + strFileExtension;
}
fileInfo = new FileInfo(strBasePath + strRealFileNM);
}
FileUpload1.PostedFile.SaveAs(fileInfo.ToString());
strReturnFile[0] = fileInfo.Name.Replace(fileInfo.Extension, "");
}
return strReturnFile;
}
//다운로드
public void AttachFile_DownLoad(string strBasePath)
{
System.IO.FileInfo fi = new System.IO.FileInfo(strBasePath);
HttpContext _context = HttpContext.Current;
// 파일이 존재하지 않는다면..
if (!fi.Exists)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ResultMsg", "<script type='text/javascript'>alert('', '해당 파일이 존재하지 않습니다.');</script>", false);
}
_context.Response.Clear(); // 출력 버퍼 초기화
_context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpContext.Current.Server.UrlPathEncode(fi.Name));
_context.Response.AddHeader("Content-Length", fi.Length.ToString()); // 파일 길이
_context.Response.ContentType = "application/x-msdownload";
_context.Response.CacheControl = "public";
_context.Response.WriteFile(fi.FullName);
_context.Response.End();
}
'SW > ASP.NET' 카테고리의 다른 글
[ASP.NET] 중첩 Repeater (Nested Repeater) (0) | 2019.12.16 |
---|---|
[ASP.NET] 비하인드 코드(cs)에서 버튼 클릭 이벤트 실행시키기 (0) | 2019.12.16 |
[ASP.NET] TextBox 입력시 다른 컨트롤 이벤트 발생시키기 (0) | 2019.12.16 |
[ASP.NET] Login & Logout - 폼 인증 (0) | 2019.09.19 |
[ASP.NET] 내장 암호화(FormsAuthenticationTicket) 기법 사용하기 - 복/부호화 (0) | 2019.09.19 |