原文链接: http://dev.mjxy.cn/a-aspnet-Upload-file.aspx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UpFile.ascx.cs" Inherits="UpFile" %>
<div style="margin: 10px;">
选择要上传的文件:<br />
<input id="UpLoadFile" type="file" runat="server" />
<input id="Button1" type="button" value="上传" runat="server" onserverclick="Button1_ServerClick" />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UpLoadFile"
ErrorMessage="请选择要上传的文件"></asp:RequiredFieldValidator>
</div>
代码文件如下:
001 | using System; |
002 | using System.Data; |
003 | using System.Configuration; |
004 | using System.Collections; |
005 | using System.Web; |
006 | using System.Web.Security; |
007 | using System.Web.UI; |
008 | using System.Web.UI.WebControls; |
009 | using System.Web.UI.WebControls.WebParts; |
010 | using System.Web.UI.HtmlControls; |
011 | using System.IO; |
012 | |
013 | /// <summary> |
014 | /// 水印用户控件 |
015 | /// 作者:成海涛 |
016 | /// 时间:2006-9-21 |
017 | /// E-mail:kingter520@163.com |
018 | /// 说明:文字水印的位置有待完善,建议用图片水印 |
019 | /// </summary> |
020 | public partial class UpFile : System.Web.UI.UserControl |
021 | { |
022 | |
023 | string _strNewFileFolder = System.Configuration.ConfigurationSettings.AppSettings[ "FCKeditor:BasePath" ]; //文件上传的目录 |
024 | string _UploadDeniedExtensions = System.Configuration.ConfigurationSettings.AppSettings[ "UploadDeniedExtensions" ]; //只允许上传的文件扩展名 ".(jpg|gif|jpeg|png)$" |
025 | int _NewImgpx = 120; //缩略图尺寸 |
026 | bool _IsThumb = true ; //是否启用缩略图 true:启用 false:不启用 |
027 | bool _IsWatermark = false ; //是否启用水印 true:启用 false:不启用 |
028 | int _WatemarkPos = 9; //水印位置 1:左上角 2:水平居中垂直顶部 3:右上角 4:垂直居中水平靠左 5:居中 6:垂直居中水平靠右 7:左下角 8:水平居中垂直底部 9:右下角 |
029 | string _WatemarkType = "txt" ; //水印类型 txt:文字水印 img:图片水印 |
030 | string _WatemarkText = "西部数字网 www.idcwest.net" ; //水印文字 |
031 | string _WatemarkFont = "Verdana" ; //水印字体 |
032 | string _WatemarkTextColor = "#FFFFFF" ; //水印文字颜色 |
033 | int _WatemarkFontSize = 18; //水印文字像素 |
034 | string _WatemarkImg = "mark.png" ; //水印图片 |
035 | int _FileSizeMax = 2048; //限制上传文件大小 |
036 | |
037 | string _srcFileName; //上传文件原文件名 |
038 | string _srcFileExtension; //扩展名 |
039 | string _strFileName; //新文件名 |
040 | string _strFileType; //文件类型 |
041 | int _strFileSize; //文件大小 单位 KB |
042 | int _OldFileWidth; //原图宽度 |
043 | int _OldFileHeight; //原图高度 |
044 | int _NewImgHeight; //缩略图高度 |
045 | int _NewImgWidth; //缩略图宽度 |
046 | string _thumbImgPath; //缩略图完整地址 |
047 | string _imagePath; //返回上传图片完整地址 |
048 | string _errMsg; //返回出错提示 |
049 | |
050 | string _strFileFolder; |
051 | int _WatemarkPosX = 5; |
052 | int _WatemarkPosY = 5; |
053 | |
054 | DateTime now = DateTime.Now; //取当前的时间到DateTime类的对象now中 |
055 | |
056 | public delegate void OnUploadEvent( object sender, EventArgs e); |
057 | public event OnUploadEvent OnUpload; |
058 | |
059 | /// <summary> |
060 | /// 设置文件上传的目录 |
061 | /// </summary> |
062 | public string NewFileFolder |
063 | { |
064 | set { _strNewFileFolder = value; } |
065 | } |
066 | |
067 | /// <summary> |
068 | /// 设置缩略图的尺寸 |
069 | /// </summary> |
070 | public int NewImgpx |
071 | { |
072 | set { _NewImgpx = value; } |
073 | } |
074 | |
075 | /// <summary> |
076 | /// 是否启用缩略图 |
077 | /// </summary> |
078 | /// |
079 | public bool IsThumb |
080 | { |
081 | set { _IsThumb = value; } |
082 | } |
083 | |
084 | /// <summary> |
085 | /// 是否启用水印 |
086 | /// </summary> |
087 | public bool IsWatermark |
088 | { |
089 | set { _IsWatermark = value; } |
090 | } |
091 | |
092 | /// <summary> |
093 | /// 设置水印位置 |
094 | /// </summary> |
095 | public int WatemarkPos |
096 | { |
097 | set { _WatemarkPos = value; } |
098 | } |
099 | |
100 | /// <summary> |
101 | /// 设置水印类型 txt:文字水印 img:图片水印 |
102 | /// </summary> |
103 | public string WatemarkType |
104 | { |
105 | set { _WatemarkType = value; } |
106 | } |
107 | |
108 | /// <summary> |
109 | /// 设置水印文字 |
110 | /// </summary> |
111 | public string WatemarkText |
112 | { |
113 | set { _WatemarkText = value; } |
114 | } |
115 | |
116 | /// <summary> |
117 | /// 设置水印字体 |
118 | /// </summary> |
119 | public string WatemarkFont |
120 | { |
121 | set { _WatemarkFont = value; } |
122 | } |
123 | |
124 | /// <summary> |
125 | /// 设置水印字号 |
126 | /// </summary> |
127 | public int WatemarkFontSize |
128 | { |
129 | set { _WatemarkFontSize = value; } |
130 | } |
131 | |
132 | /// <summary> |
133 | /// 设置水印文字颜色 |
134 | /// </summary> |
135 | public string WatemarkTextColor |
136 | { |
137 | set { _WatemarkTextColor = value; } |
138 | } |
139 | /// <summary> |
140 | /// 设置水印图片 |
141 | /// </summary> |
142 | public string WatemarkImg |
143 | { |
144 | set { _WatemarkImg = value; } |
145 | } |
146 | /// <summary> |
147 | /// 设置上传文件的限制大小 |
148 | /// </summary> |
149 | public int FileSizeMax |
150 | { |
151 | set { _FileSizeMax = value; } |
152 | } |
153 | |
154 | /// <summary> |
155 | /// 上传文件原文件名 |
156 | /// </summary> |
157 | public string OldFileName |
158 | { |
159 | get { return _srcFileName; } |
160 | } |
161 | |
162 | /// <summary> |
163 | /// 扩展名 |
164 | /// </summary> |
165 | public string OldFileExtension |
166 | { |
167 | get { return _srcFileExtension; } |
168 | } |
169 | |
170 | /// <summary> |
171 | /// 新文件名 |
172 | /// </summary> |
173 | public string FileName |
174 | { |
175 | get { return _strFileName; } |
176 | } |
177 | |
178 | /// <summary> |
179 | /// 文件类型 |
180 | /// </summary> |
181 | public string FileType |
182 | { |
183 | get { return _strFileType; } |
184 | } |
185 | /// <summary> |
186 | /// 文件大小 单位 KB |
187 | /// </summary> |
188 | public int FileSize |
189 | { |
190 | get { return _strFileSize; } |
191 | } |
192 | |
193 | /// <summary> |
194 | /// 原图宽度 |
195 | /// </summary> |
196 | public int OldFileWidth |
197 | { |
198 | get { return _OldFileWidth; } |
199 | } |
200 | |
201 | /// <summary> |
202 | /// 原图高度 |
203 | /// </summary> |
204 | public int OldFileHeight |
205 | { |
206 | get { return _OldFileHeight; } |
207 | } |
208 | |
209 | /// <summary> |
210 | /// 缩略图高度 |
211 | /// </summary> |
212 | public int ThumbImageHeight |
213 | { |
214 | get { return _NewImgHeight; } |
215 | } |
216 | |
217 | /// <summary> |
218 | /// 缩略图宽度 |
219 | /// </summary> |
220 | public int ThumbImageWidth |
221 | { |
222 | get { return _NewImgWidth; } |
223 | } |
224 | |
225 | /// <summary> |
226 | /// 缩略图完整地址 |
227 | /// </summary> |
228 | public string ThumbImagePath |
229 | { |
230 | get { return _thumbImgPath; } |
231 | } |
232 | |
233 | /// <summary> |
234 | /// 返回上传图片完整地址 |
235 | /// </summary> |
236 | public string FilePath |
237 | { |
238 | get { return _imagePath; } |
239 | } |
240 | |
241 | /// <summary> |
242 | /// 返回出错提示 |
243 | /// </summary> |
244 | public string ErrMsg |
245 | { |
246 | get { return _errMsg; } |
247 | } |
248 | |
249 | protected void Page_Load( object sender, EventArgs e) |
250 | { |
251 | _WatemarkImg = Server.MapPath( "~/" ) + _WatemarkImg; //水印图片 |
252 | |
253 | //文件上传的目录 |
254 | _strNewFileFolder += now.Year.ToString() + "/" ; |
255 | _strFileFolder = Server.MapPath( "~/" ) + _strNewFileFolder; |
256 | } |
257 | |
258 | protected void Button1_ServerClick( object sender, EventArgs e) |
259 | { |
260 | //随机数 |
261 | Random Rnd = new Random(); |
262 | int strRnd = Rnd.Next(1, 99); |
263 | |
264 | //新文件名称 |
265 | string strFileName = now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + strRnd.ToString(); |
266 | |
267 | if (UpLoadFile.PostedFile.ContentLength != 0) //判断选取对话框的文件长度是否为0 |
268 | { |
269 | |
270 | //检测目录是否存在,不存在就建立目录 |
271 | if (!Directory.Exists(_strFileFolder)) |
272 | { |
273 | Directory.CreateDirectory(_strFileFolder); |
274 | } |
275 | |
276 | //获取原文件信息 |
277 | string srcFile = UpLoadFile.PostedFile.FileName; |
278 | string srcFileName = Path.GetFileName(srcFile.ToString()); //原文件名 |
279 | string srcFileExtension = Path.GetExtension(srcFile.ToString()); //原文件扩展名 |
280 | string strFileType = UpLoadFile.PostedFile.ContentType; //获得文件类型 |
281 | int strFileSize = UpLoadFile.PostedFile.ContentLength / _FileSizeMax; //获得文件体积大小,单位KB |
282 | |
283 | |
284 | //限制文件上传大小 |
285 | if (strFileSize > _FileSizeMax) |
286 | { |
287 | _errMsg = "上传文件超过限制,文件大小限制为:" + _FileSizeMax + " KB" ; |
288 | Response.Write( "<script>alert('" + _errMsg + "')</script>" ); |
289 | return ; |
290 | } |
291 | if (!System.Text.RegularExpressions.Regex.IsMatch(srcFileExtension, _UploadDeniedExtensions, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) |
292 | { |
293 | _errMsg = "不允许上传此类型的文件" ; |
294 | Response.Write( "<script>alert('" + _errMsg + "')</script>" ); |
295 | return ; |
296 | } |
297 | strFileName = strFileName + srcFileExtension; //新文件名包括扩展名 |
298 | _imagePath = _strNewFileFolder + strFileName; //返回上传后的图片路径 |
299 | |
300 | //输出 |
301 | _srcFileName = srcFileName; //上传文件原文件名 |
302 | _srcFileExtension = srcFileExtension; //扩展名 |
303 | _strFileName = strFileName; //新文件名 |
304 | _strFileType = strFileType; //文件类型 |
305 | _strFileSize = strFileSize; //文件大小 单位 KB |
306 | |
307 | //判断是否为图片 |
308 | bool IsImg = true ; |
309 | if (UpLoadFile.PostedFile.ContentType.IndexOf( "image" ) < 0) |
310 | { |
311 | IsImg = false ; |
312 | } |
313 | |
314 | if (IsImg == false ) |
315 | { |
316 | UpLoadFile.PostedFile.SaveAs(_strFileFolder + strFileName); //非图片文件上传 |
317 | } |
318 | else |
319 | { |
320 | //加水印并上传 |
321 | if (_IsWatermark == true ) |
322 | { |
323 | |
324 | System.Drawing.Image image; |
325 | if (strFileType == "image/bmp" ) //bmp图片格式 |
326 | image = System.Drawing.Bitmap.FromStream(UpLoadFile.PostedFile.InputStream); |
327 | else |
328 | image = System.Drawing.Image.FromStream(UpLoadFile.PostedFile.InputStream); |
329 | |
330 | System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); |
331 | |
332 | if (_WatemarkType == "txt" ) //文字水印 |
333 | { |
334 | |
335 | //将图片绘制到graphics中 |
336 | g.DrawImage(image, 0, 0, image.Width, image.Height); |
337 | //设置文字的属性 |
338 | System.Drawing.Font f = new System.Drawing.Font(_WatemarkFont, _WatemarkFontSize, System.Drawing.GraphicsUnit.Pixel); |
339 | |
340 | |
341 | //判断图片的大小,如果图片过小,不写文字 |
342 | if (image.Width < 200) |
343 | //在此设定在图片上所加的文字 |
344 | _WatemarkText = "" ; |
345 | |
346 | //写的文字的起始位置,x,y坐标 |
347 | _WatemarkPosX = 5; |
348 | _WatemarkPosY = 5; |
349 | //设置字体的颜色 |
350 | System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.ColorTranslator.FromHtml(_WatemarkTextColor)); |
351 | |
352 | System.Drawing.StringFormat fm = new System.Drawing.StringFormat(); |
353 | |
354 | //水印放置左上角 |
355 | if (_WatemarkPos == 1) |
356 | { |
357 | fm.Alignment = System.Drawing.StringAlignment.Near; |
358 | fm.LineAlignment = System.Drawing.StringAlignment.Near; |
359 | } |
360 | |
361 | //水平居中垂直顶部 |
362 | if (_WatemarkPos == 2) |
363 | { |
364 | fm.Alignment = System.Drawing.StringAlignment.Center; |
365 | fm.LineAlignment = System.Drawing.StringAlignment.Near; |
366 | } |
367 | |
368 | //右上角 |
369 | if (_WatemarkPos == 3) |
370 | { |
371 | fm.Alignment = System.Drawing.StringAlignment.Far; |
372 | fm.LineAlignment = System.Drawing.StringAlignment.Near; |
373 | |
374 | } |
375 | |
376 | //垂直居中水平靠左 |
377 | if (_WatemarkPos == 4) |
378 | { |
379 | fm.Alignment = System.Drawing.StringAlignment.Near; |
380 | fm.LineAlignment = System.Drawing.StringAlignment.Center; |
381 | } |
382 | |
383 | //居中 |
384 | if (_WatemarkPos == 5) |
385 | { |
386 | fm.Alignment = System.Drawing.StringAlignment.Center; |
387 | fm.LineAlignment = System.Drawing.StringAlignment.Center; |
388 | } |
389 | |
390 | //垂直居中水平靠右 |
391 | if (_WatemarkPos == 6) |
392 | { |
393 | fm.Alignment = System.Drawing.StringAlignment.Far; |
394 | fm.LineAlignment = System.Drawing.StringAlignment.Center; |
395 | } |
396 | |
397 | //左下角 |
398 | if (_WatemarkPos == 7) |
399 | { |
400 | fm.Alignment = System.Drawing.StringAlignment.Near; |
401 | fm.LineAlignment = System.Drawing.StringAlignment.Far; |
402 | } |
403 | |
404 | //水平居中垂直底部 |
405 | if (_WatemarkPos == 8) |
406 | { |
407 | fm.Alignment = System.Drawing.StringAlignment.Center; |
408 | fm.LineAlignment = System.Drawing.StringAlignment.Far; |
409 | } |
410 | |
411 | //水印放置右下角 |
412 | if (_WatemarkPos == 9) |
413 | { |
414 | fm.Alignment = System.Drawing.StringAlignment.Far; |
415 | fm.LineAlignment = System.Drawing.StringAlignment.Far; |
416 | } |
417 | |
418 | |
419 | //写字 |
420 | g.DrawString(_WatemarkText, f, b, new System.Drawing.RectangleF(0, 0, image.Width, image.Height), fm); |
421 | |
422 | //释放graphics |
423 | g.Dispose(); |
424 | |
425 | //保存加上水印的图片 |
426 | image.Save(_strFileFolder + strFileName); |
427 | //释放image |
428 | image.Dispose(); |
429 | |
430 | } |
431 | else if (_WatemarkType == "img" ) //图片水印 |
432 | { |
433 | |
434 | System.Drawing.Image copyImage = System.Drawing.Image.FromFile(_WatemarkImg); |
435 | //水印放置左上角 |
436 | if (_WatemarkPos == 1) |
437 | { |
438 | _WatemarkPosX = 5; |
439 | _WatemarkPosY = 5; |
440 | } |
441 | |
442 | //水平居中垂直顶部 |
443 | if (_WatemarkPos == 2) |
444 | { |
445 | _WatemarkPosX = (image.Width - copyImage.Width) / 2; |
446 | _WatemarkPosY = 5; |
447 | } |
448 | |
449 | //右上角 |
450 | if (_WatemarkPos == 3) |
451 | { |
452 | _WatemarkPosX = image.Width - copyImage.Width - 5; |
453 | _WatemarkPosY = 5; |
454 | } |
455 | |
456 | //垂直居中水平靠左 |
457 | if (_WatemarkPos == 4) |
458 | { |
459 | _WatemarkPosX = 5; |
460 | _WatemarkPosY = (image.Height - copyImage.Height) / 2; |
461 | } |
462 | |
463 | //居中 |
464 | if (_WatemarkPos == 5) |
465 | { |
466 | _WatemarkPosX = (image.Width - copyImage.Width) / 2; |
467 | _WatemarkPosY = (image.Height - copyImage.Height) / 2; |
468 | } |
469 | |
470 | //垂直居中水平靠右 |
471 | if (_WatemarkPos == 6) |
472 | { |
473 | _WatemarkPosX = image.Width - copyImage.Width - 5; |
474 | _WatemarkPosY = (image.Height - copyImage.Height) / 2; |
475 | } |
476 | |
477 | //左下角 |
478 | if (_WatemarkPos == 7) |
479 | { |
480 | _WatemarkPosX = 5; |
481 | _WatemarkPosY = image.Height - copyImage.Height - 5; |
482 | } |
483 | |
484 | //水平居中垂直底部 |
485 | if (_WatemarkPos == 8) |
486 | { |
487 | _WatemarkPosX = (image.Width - copyImage.Width) / 2; |
488 | _WatemarkPosY = image.Height - copyImage.Height - 5; |
489 | } |
490 | |
491 | //水印放置右下角 |
492 | if (_WatemarkPos == 9) |
493 | { |
494 | _WatemarkPosX = image.Width - copyImage.Width - 5; |
495 | _WatemarkPosY = image.Height - copyImage.Height - 5; |
496 | } |
497 | |
498 | if (copyImage.Height < image.Height && copyImage.Width < image.Width) |
499 | { |
500 | //将水印打印到上传图片上去 |
501 | g.DrawImage(copyImage, new System.Drawing.Rectangle(_WatemarkPosX, _WatemarkPosY, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel); |
502 | //释放graphics |
503 | g.Dispose(); |
504 | } |
505 | //保存图片 |
506 | image.Save(_strFileFolder + strFileName); |
507 | //释放image |
508 | image.Dispose(); |
509 | } |
510 | |
511 | } |
512 | else |
513 | { |
514 | UpLoadFile.PostedFile.SaveAs(_strFileFolder + strFileName); //不加水印上传 |
515 | } |
516 | |
517 | //获得图片尺寸 |
518 | int OldFileHeight, OldFileWidth; |
519 | System.Drawing.Image img = System.Drawing.Image.FromStream(UpLoadFile.PostedFile.InputStream); |
520 | OldFileHeight = img.Height; |
521 | OldFileWidth = img.Width; |
522 | |
523 | //创建缩略图 |
524 | if (_IsThumb == true ) |
525 | { |
526 | int NewImgHeight, NewImgWidth; |
527 | |
528 | if (OldFileWidth > OldFileHeight) |
529 | { |
530 | NewImgWidth = _NewImgpx; |
531 | NewImgHeight = ( int )(OldFileHeight / ( float )OldFileWidth * _NewImgpx); |
532 | } |
533 | else |
534 | { |
535 | NewImgHeight = _NewImgpx; |
536 | NewImgWidth = ( int )(OldFileWidth / ( float )OldFileHeight * _NewImgpx); |
537 | } |
538 | //如果缩略图的尺寸大于原图尺寸,则用原图片大小 |
539 | if (NewImgHeight >= OldFileHeight && NewImgWidth >= OldFileWidth) |
540 | { |
541 | NewImgHeight = OldFileHeight; |
542 | NewImgWidth = OldFileWidth; |
543 | } |
544 | System.Drawing.Image NewImg = img.GetThumbnailImage(NewImgWidth, NewImgHeight, null , System.IntPtr.Zero); |
545 | NewImg.Save(_strFileFolder + "Thumb_" + strFileName, System.Drawing.Imaging.ImageFormat.Jpeg); |
546 | |
547 | //返回数据 |
548 | _NewImgHeight = NewImgHeight; //缩略图高度 |
549 | _NewImgWidth = NewImgWidth; //缩略图宽度 |
550 | _thumbImgPath = _strNewFileFolder + "Thumb_" + strFileName; //缩略图地址 |
551 | } |
552 | //返回数据 |
553 | _OldFileWidth = OldFileWidth; //原图宽度 |
554 | _OldFileHeight = OldFileHeight; //原图高度 |
555 | |
556 | } |
557 | } |
558 | |
559 | this .OnUpload( this , e); |
560 | |
561 | } |
562 | } |
具体使用方式: