SWFUpload多文件上传,文件大小增大问题

因项目需要,内容编辑希望增加多文件上传功能,我们采用了SWFUpload

SWFUpload下载地址http://swfupload.googlecode.com/
SWFUpload下载档案:SWFUpload-Samples v2.1.0.Release.zip

在\SWFUpload Samples v2.1.0\demos\applicationdemo.net目录下相应的asp.net源码

源码只是演示了把缩略图信息保存到一个List里面,并定义了一个Thumbnail类。

首先,我修改了部分源码,先选择图片,然后保存图片,这样万一编辑选择错误了,可以重新选择,如图所示:

 

我重新改写了Thumbnail,变成了ImageInfo类,代码如下
public ImageInfo(PicTypeUtility picType, int picSize, int picH, int picW, int picTempValue, string picName, byte[] data)
{
this.picType = picType;
this.picSize = picSize;
this.picHeight = picH;
this.picWidth = picW;
this.picTempValue = picTempValue;
this.picName = picName;
this.data = data;
}
private PicTypeUtility picType;
public PicTypeUtility PicType
{
get { return picType; }
set { picType = value; }
}

private int picSize;
public int PicSize
{
get { return picSize; }
set { picSize = value; }
}

private int picHeight;
public int PicHeight
{
get { return picHeight; }
set { picHeight = value; }
}

private int picWidth;
public int PicWidth
{
get { return picWidth; }
set { picWidth = value; }
}

private int picTempValue;
public int PicTempValue
{
get { return picTempValue; }
set { picTempValue = value; }
}

private string picName;
public string PicName
{
get { return picName; }
set { picName = value; }
}

private byte[] data;
public byte[] Data
{
get { return this.data; }
set { this.data = value; }
}

那么相应的
Thumbnail thumb = new Thumbnail(thumbnail_id, ms.GetBuffer()); 需要修改为:
ImageInfo imageInfo = new ImageInfo(picType, picSize, picHeight, picWidth, 0, picName, ms.GetBuffer());

保存的时候,代码如下:
FileStream fs = new FileStream(Server.MapPath(into.PicName), FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(info.Data);
bw.Close();
fs.Close();

在实际使用中,发现某些图片,保存到文件夹后,尺寸竟然变成了双倍大小。一种65k左右的图片,保存后达到128k。

对代码做了如下修改
修改ImageInfo类
增加Stream SM属性
private System.IO.Stream sm;
public System.IO.Stream SM
{
get { return this.sm; }
set { this.sm = value; }
}
增加以下构造函数
public ImageInfo(PicTypeUtility picType, int picSize, int picH, int picW, int picTempValue, string picName, System.IO.Stream sm)
{
this.picType = picType;
this.picSize = picSize;
this.picHeight = picH;
this.picWidth = picW;
this.picTempValue = picTempValue;
this.picName = picName;
this.sm = sm;
}

并修改upload.aspx
HttpPostedFile imgUpload = Request.Files["Filedata"];
ImageInfo imageInfo = new ImageInfo(picType, picSize, picHeight, picWidth, 0, picName, imgUpload.InputStream);

保存的时候,代码修改如下:
System.Drawing.Image image = System.Drawing.Image.FromStream(info.SM);
image.Save(Server.MapPath(info.PicName));
image.Dispose();

最后,请记住为ImageInfo添加可系列化属性[Serializable()],这个可别忘了

测试,一切正常!

posted @ 2009-04-16 14:32  对工作要鞠躬尽瘁  阅读(5642)  评论(4编辑  收藏  举报