upload.html 파일
<!doctype html> <html> <head> <meta charset="utf-8"> <title>파일업로드</title> </head> <body> <form name="form1" method="post" action="Upload.aspx" enctype="multipart/form-data"> <input type="file" name="file1" multiple/> <input type="submit" value="업로드"/> </form> </body> </html>
Upload.aspx 파일
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Upload.aspx.cs" Inherits="Upload" %>
Upload.aspx.cs 파일
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public partial class Upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection uploadedFiles = Request.Files;
int maxFile = 10;
if (uploadedFiles.Count > maxFile)
{
Response.Write("한번에 올릴 수 있는 파일수는 " + maxFile.ToString() + "개입니다.");
return;
}
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
if (userPostedFile.ContentLength > 0 && !String.IsNullOrEmpty(userPostedFile.FileName))
{
string result = UploadFileSave(userPostedFile, i);
if (result != "")
{
// 업로드 성공
Response.Write(result + " ");
}
else
{
// 업로드 실패
}
}
}
}
private string UploadFileSave(HttpPostedFile SaveFile, int no)
{
// 저장폴더
string wPath = Server.MapPath("/file") + "\\";
string thumPath = Server.MapPath("/file/thum") + "\\";
DirectoryInfo di = new DirectoryInfo(wPath);
// 폴더가 없을 경우 생성
if (di.Exists == false)
di.Create();
di = new DirectoryInfo(thumPath);
// 폴더가 없을 경우 생성
if (di.Exists == false)
di.Create();
// 확장자
string fileType = SaveFile.FileName.Substring(SaveFile.FileName.LastIndexOf(".") + 1);
// 변경할 파일 이름
string saveFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + '_' + no + "." + fileType;
try
{
// 이미지 확장자 일 경우
if (("jpg|jpeg|bmp|gif|png").IndexOf(fileType.ToLower()) > -1)
{
Bitmap img = new Bitmap(SaveFile.InputStream);
RotateFlipType rft = RotateFlipType.RotateNoneFlipNone;
PropertyItem[] properties = img.PropertyItems;
foreach (PropertyItem p in properties)
{
// 이미지가 똑바로 되어있지 않은 경우
if (p.Id == 274)
{
Int16 orientation = BitConverter.ToInt16(p.Value, 0);
switch (orientation)
{
case 1:
rft = RotateFlipType.RotateNoneFlipNone;
break;
case 3:
rft = RotateFlipType.Rotate180FlipNone;
break;
case 4:
rft = RotateFlipType.Rotate90FlipNone;
break;
case 8:
rft = RotateFlipType.Rotate270FlipNone;
break;
}
}
}
// 이미지가 부분은 이미지가 누워있을 경우 세워준다
if (rft != RotateFlipType.RotateNoneFlipNone)
{
img.RotateFlip(rft);
}
// 저장
img.Save(wPath + "\\" + saveFileName);
// 썸네일 작업
// 썸네일 폭
int thumW = 100;
// 썸네일 높이
int thumH = 100;
// 썸네일 좌표 X
int thumX = 0;
// 썸네일 좌표 Y
int thumY = 0;
// 썸네일 이미지 그리기전 사이즈 지정
Bitmap thumimg = new Bitmap(thumW, thumH);
if (img.Width >= thumW && img.Height >= thumH)
{
if (img.Width >= img.Height)
{
thumW = (thumH * img.Width) / img.Height;
thumX = (thumW - thumH) / 2;
thumY = 0;
}
else
{
thumH = (thumW * img.Height) / img.Width;
thumY = (thumH - thumW) / 2;
thumX = 0;
}
}
else if (img.Width >= img.Height && img.Height < thumH)
{
thumW = img.Width;
thumH = img.Height;
thumX = (img.Width - thumW) / 2;
thumY = (img.Height - thumH) / 2 * -1;
}
else if (img.Width < img.Height && img.Height >= thumH)
{
thumW = img.Width;
thumH = img.Height;
thumX = (img.Width - thumW) / 2 * -1;
thumY = (img.Height - thumH) / 2;
}
else
{
thumW = img.Width;
thumH = img.Height;
thumX = (img.Width - thumW) / 2 * -1;
thumY = (img.Height - thumH) / 2 * -1;
}
Graphics gp = Graphics.FromImage(thumimg);
gp.InterpolationMode = InterpolationMode.HighQualityBicubic;
gp.DrawImage(img, new Rectangle(-thumX, -thumY, thumW, thumH));
// 썸네일 저장
thumimg.Save(wPath + "\\thum\\" + saveFileName);
}
else
{
// 이미지가 아닐경우
// 저장하기
SaveFile.SaveAs(wPath + "\\" + saveFileName);
}
}
catch
{
return "";
}
return saveFileName;
}
}
111
답글삭제이거 댓글수정을 모르겠네요!ㅜㅜ 좋은 글 잘봤습니다!
삭제