upload.html 파일
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!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 파일
1 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Upload.aspx.cs" Inherits="Upload" %> |
Upload.aspx.cs 파일
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | 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
답글삭제이거 댓글수정을 모르겠네요!ㅜㅜ 좋은 글 잘봤습니다!
삭제