using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace UpLoadFile
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private static Object ForCreateGUIDLock = new Object();
public static string CreateGUID()
{
lock (ForCreateGUIDLock)
{
return Guid.NewGuid().ToString("N");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string filePath = FileUpload1.PostedFile.FileName;
string fileName = CreateGUID() + filePath.Substring(filePath.LastIndexOf("."));
Server.MapPath("~/fileUpload");//ConfigurationManager.AppSettings["fileUploadPath"];
//"E:\\uploadfile";//Server.MapPath("~/Picture");
string sourcePath = path + "\\source\\"; //源文件路径ConfigurationManager.AppSettings["source"];//
string middlePath = path + "\\middle\\"; //中号的图片路径ConfigurationManager.AppSettings["middel"];//
string smallPath = path + "\\small\\"; //小图片路径ConfigurationManager.AppSettings["small"];//
string year = DateTime.Now.Year.ToString();
string mouth = DateTime.Now.Month.ToString();
string day = DateTime.Now.Day.ToString();
sourcePath = sourcePath + year + "\\" + mouth + "\\" + day + "\\";
middlePath = middlePath + year + "\\" + mouth + "\\" + day + "\\";
smallPath = smallPath + year + "\\" + mouth + "\\" + day + "\\";
if (!Directory.Exists(path))
{ //判断是否存在uploadfile路径
Directory.CreateDirectory(path);
}
if (!Directory.Exists(sourcePath)) //判断 、创建原图片路径
{
Directory.CreateDirectory(sourcePath);
}
//上传原图片
FileUpload1.SaveAs(sourcePath + fileName);
//上传小图片
UploadImage(fileName, sourcePath, smallPath, 100, 150);
//上传中图片
UploadImage(fileName, sourcePath, middlePath, 400, 400);
Response.Write("<script>alert('上传文件成功!')</script>");
}
catch (Exception)
{
Response.Write("<script>alert('上传文件失败!')</script>");
}
}
private void UploadImage(string fileName, string sourcePath, string path, int newWidth, int newHeight)
{
//得到小图片Image对象
System.Drawing.Image smallImage = GetNewImage(sourcePath + fileName, newWidth, newHeight);
if (!Directory.Exists(path)) //判断、创建小图片的存放路径
{
Directory.CreateDirectory(path);
}
smallImage.Save(path + fileName);
}
/// <summary>
/// 对图片进行处理,返回一个Image类别的对象
/// </summary>
/// <param name="oldImgPath">原图片路径</param>
/// <param name="newWidth">新图片宽度</param>
/// <param name="newHeight">新图片高度</param>
/// <returns></returns>
private System.Drawing.Image GetNewImage(string oldImgPath, int newWidth, int newHeight)
{
System.Drawing.Image newImage = null;
System.Drawing.Image oldImage = null;
oldImage = System.Drawing.Image.FromFile(oldImgPath); //加载原图片
newImage = oldImage.GetThumbnailImage(newWidth, newHeight,
new System.Drawing.Image.GetThumbnailImageAbort(IsTrue), IntPtr.Zero); //对原图片进行缩放
return newImage;
}
/// <summary>
/// 在Image类别对图片进行缩放的时候,需要一个返回bool类别的委托
/// </summary>
/// <returns></returns>
private bool IsTrue() {
return true;
}
}
}