打包上传图片

出于工作需要,在我的一个项目中,需要对图片进行批量上传. 传统的批量上传,一般常见的就是写N个上<input type="file"> 这样的控件, 然后循环得到需要上传的东西, 以此达到批量上传的目录.

在.net中, 需要上传图片已经是变得相当轻松了.  昨日我在项目中实现了上传压缩文件,然后对压缩文件进行解压,再对解压出来的文件进行上传的批量上传方法.

以压缩文件的方式上传图片,流程就是是这样的:

  1. 对需要上传的*.jpg等文件进行打包成a.zip
  2. 上传此a.zip压缩文件
  3. 在服务器上解压此a.zip压缩文件,解压到一个临时文件夹tempFolder中.
  4. 遍历临时文件夹tempFolder中的所有*.jpg文件. 并上传找到的*.jpg文件.
  5. 删除临时文件夹tempFolder
  6. 完毕

以下这个过程的代码:

using System;
using System.IO;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

using Wap.Components.Components;
using Wap.Components.FileOrImageUpload;

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

namespace Wap.Components.Album
{
    
#region 相册上传类的属性访问器
    
public class UploadAlbum
    {
        
public UploadAlbum()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
/// <summary>
        
/// 上传标题
        
/// </summary>
        private string title;
        
public string Title
        {
            
get
            {
                
return this.title;
            }
            
set
            {
                
this.title            = value;
            }
        }

        
/// <summary>
        
/// 上传时的自定义宽度
        
/// </summary>
        private int width;
        
public int Width
        {
            
get
            {
                
return this.width;
            }
            
set
            {
                
this.width            = value;
            }
        }

        
/// <summary>
        
/// 上传时的自定义高度
        
/// </summary>
        private int height;
        
public int Height
        {
            
get
            {
                
return this.height;
            }
            
set
            {
                
this.height            = value;
            }
        }

        
/// <summary>
        
/// 文件扩展名
        
/// </summary>
        private string fileExtension;
        
public string FileExtension
        {
            
get
            {
                
return this.fileExtension;
            }
            
set
            {
                
this.fileExtension    = value;
            }
        }

        
/// <summary>
        
/// 相册栏目ID
        
/// </summary>
        private int albumClassID;
        
public int AlbumClassID
        {
            
get
            {
                
return this.albumClassID;
            }
            
set
            {
                
this.albumClassID    = value;
            }
        }
    }

    
#endregion

    
#region 上传类的逻辑操作
    
public class UploadOp
    {
        
public UploadOp()
        {
        
        }

        
/// <summary>
        
/// 上传
        
/// </summary>
        
/// <param name="uploadAlbum"></param>
        
/// <param name="postedFile"></param>
        public void Upload(UploadAlbum uploadAlbum, HttpPostedFile postedFile)
        {
            
string fileName                    = postedFile.FileName;
            
int size                        = postedFile.ContentLength;
            
string fileExtension;
            fileExtension                    
= Path.GetExtension(fileName).ToLower();

            CheckExtensionIsEnable(fileExtension);

            CheckSize(size, fileExtension);

            
// 判断完毕后,开始上传
            Upload(fileExtension, postedFile, uploadAlbum);
        }

        
private void Upload(string fileExtension, HttpPostedFile postedFile, UploadAlbum uploadAlbum)
        {
            
string originalPath                = postedFile.FileName;

            
if(fileExtension == ".zip")
            {
                Random random                
= new Random();
                
string fileName                = Path.GetFileName(postedFile.FileName);
                
string tempFolder            = Globals.PhysicalApplicationPath() + "Temp" + random.Next(100);
                
string tempSaveFolder        = tempFolder + "\\" + fileName;
                Directory.CreateDirectory(tempFolder);
                postedFile.SaveAs(tempSaveFolder);

                
// 然后开始对临时文件进行解压
                Wap.Components.Album.Upload.UnZip(tempSaveFolder);

                
// 解压完毕后,开始上传解压出来的文件。
                DirectoryInfo di            = new DirectoryInfo(tempFolder);
                FileInfo[] fis                
= di.GetFiles("*.jpg");
                
foreach(FileInfo fi in fis)
                {
                    Wap.Components.Album.Upload.MaikThumb(fi.FullName, 
@"E:\666666.jpg", uploadAlbum);
                }

                
// 解压出来的文件上传后,删除临时文件夹
                if(Directory.Exists(tempFolder))
                {
                    Directory.Delete(tempFolder, 
true);
                }
            }
            
else if(fileExtension == ".jpg")
            {
                Wap.Components.Album.Upload.MaikThumb(originalPath, 
@"E:\555555.jpg", uploadAlbum);
            }
        }

        
/// <summary>
        
/// 检测上传格式是否允许
        
/// </summary>
        
/// <param name="fileExtension"></param>
        private static void CheckExtensionIsEnable(string fileExtension)
        {
            SetUploadParameter uploadP        
= new SetUploadParameter();

            
bool fileExtensionIsEnable        = false;
            
string fileExtensionEnable        = AlbumSiteSettings.FileExtension;
            fileExtensionIsEnable            
= uploadP.CheckExtensionIsAllowable(fileExtension, fileExtensionEnable);

            
if(!fileExtensionIsEnable)
            {
                HttpContext.Current.Response.Write(
"<script>alert('上传格式不允许,允许的格式如下:" + fileExtension + "');window.history.go(-1);</script>");
                HttpContext.Current.Response.End();
            }
        }

        
/// <summary>
        
/// 检测上传的文件大小
        
/// </summary>
        
/// <param name="size"></param>
        private static void CheckSize(int size, string fileExtension)
        {
            
bool isOverSize                    = false;
            
if(fileExtension == ".zip")
            {
                
if(size>AlbumSiteSettings.ZipUploadSize)
                {
                    isOverSize                
= true;
                }
            }
            
else
            {
                
if(size>AlbumSiteSettings.UploadSize)
                {
                    isOverSize                
= true;
                }
            }

            
if(isOverSize)
            {
                HttpContext.Current.Response.Write(
"<script>alert('上传大小超出限制:zip打包文件大小为" + AlbumSiteSettings.ZipUploadSize + " 其它的为:" + AlbumSiteSettings.UploadSize + "');window.history.go(-1);</script>");
                HttpContext.Current.Response.End();
            }
        }
    }

    
#endregion
    
    
#region 上传的上传类
    
public class Upload
    {
        
public Upload()
        {
        
        }

        
/// <summary>
        
/// 开始上传图片
        
/// </summary>
        
/// <param name="originalPath"></param>
        
/// <param name="thumbPath"></param>
        
/// <param name="uploadAlbum"></param>
        public static void MaikThumb(string originalPath, string thumbPath, UploadAlbum uploadAlbum)
        {
            Image originalImage            
= Image.FromFile(originalPath);

            
int originalWidth            = originalImage.Width;
            
int originalHeight            = originalImage.Height;

            
int newWidth                = uploadAlbum.Width;
            
int newHeight                = uploadAlbum.Height;

            
// 如果没有指定宽度和高度,那么就以原始图象的宽度和高度为准
            if(newWidth == 0)
            {
                
if(originalWidth>1024)
                {
                    newWidth            
= 1024;
                }
                
else
                {
                    newWidth            
= originalWidth;
                }
            }

            
if(newHeight == 0)
            {
                
if(originalHeight>1024)
                {
                    newHeight            
= 1024;
                }
                
else
                {
                    newHeight            
= originalHeight;
                }
            }

            
// 在此进行等比例判断,公式如下:
            
// a    c
            
// — = —
            
// b    d
            if( (double)originalWidth/(double)originalHeight > (double)newWidth/(double)newHeight )
            {
                newHeight                
= originalHeight*newWidth / originalWidth;
            }
            
else
            {
                newWidth                
= originalWidth*newHeight/originalHeight;
            }

            Image bitmap                
= new System.Drawing.Bitmap(newWidth, newHeight);
            Graphics g                    
= Graphics.FromImage(bitmap);
            g.SmoothingMode                
= SmoothingMode.HighQuality;
            g.InterpolationMode            
= InterpolationMode.High;
            g.Clear(System.Drawing.Color.Transparent);

            g.DrawImage(originalImage, 
new Rectangle(00, newWidth, newHeight), new Rectangle(00, originalWidth, originalHeight), GraphicsUnit.Pixel);

            
try
            {
                bitmap.Save(thumbPath, ImageFormat.Jpeg);
            }
            
catch(System.Exception e)
            {
                
throw e;
            }
            
finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }

        
/// <summary>
        
/// 解压指定的.zip文件
        
/// </summary>
        
/// <param name="zipFile"></param>
        public static void UnZip(string zipFile)
        {
            ZipInputStream zis                    
= new ZipInputStream(File.OpenRead(zipFile));

            ZipEntry zipEntry;
            
string folderName                    = Path.GetDirectoryName(zipFile) + "\\";

            
while((zipEntry = zis.GetNextEntry()) != null)
            {
                
string fileName                    = Path.GetFileName(zipEntry.Name);

                
if(fileName != "")
                {
                    FileStream fs                
= File.Create(folderName + fileName);
                    
byte[] buffer                = new byte[1024];
                    
while(true)
                    {
                        
int size                = zis.Read(buffer, 0, buffer.Length);

                        
if(size>0)
                        {
                            fs.Write(buffer, 
0, size);
                        }
                        
else
                        {
                            
break;
                        }
                    }

                    fs.Close();
                }
            }

            zis.Close();
        }    
    }
    
#endregion
}

-------------------
本程序已经通过.  希望该上传方法对对大家有所帮助.
http://www.cnblogs.com/mazhiyuan/archive/2006/04/26/385810.html
posted on 2007-02-05 17:28  mbskys  阅读(457)  评论(0)    收藏  举报