FreeTextBox图片库上传文件重命名及判断文件类型

最近用到一个小项目用到FreeTextBox,发现图片上传这块确实有很多不太方便的地方,

网上查了很多资料,特别是看到dudu几年前的那篇文章, 顺手就拿过来用了,

昨天晚上想到要加一个自动重命名的功能, 于是就结合反编译的源码改出了下面的代码

实现很简单, 就是从ImageGallery继承了一个新的图片上传控件

然后修改ftb.imagegallery.aspx中的代码,使用自己继承过来的控件

收藏一下(FreeTextBox用的是3.2.4)

 

 

FTBImageGallery
public class FTBImageGallery : ImageGallery
{
public FTBImageGallery()
{
}

public override void RaisePostBackEvent(string eventArgument)
{
char[] chArray1 = new char[] { ':' };
string[] textArray1 = eventArgument.Split(chArray1);
if (textArray1[0] != null && textArray1[0] == "UploadImage")
{
this.EnsureChildControls();

if (!this.AllowImageUpload)
{
this.returnMessage = "不允许上传";
return;
}
if (this.inputFile == null)
{
this.returnMessage = "Error: InputFile control not yet created!";
return;
}
if ((this.inputFile.PostedFile == null) || (this.inputFile.PostedFile.FileName == null))
{
throw new Exception("请选择需要上传的图片");
}
if (!(IsAcceptedFileTypes(this.inputFile.PostedFile.FileName)))
{
this.returnMessage = "不允许上传该类型的图片";
return;
}

string newFileName = Guid.NewGuid() + Path.GetExtension(this.inputFile.PostedFile.FileName);
this.inputFile.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(this.CurrentImagesFolder) + @"\" + newFileName);
this.returnMessage = "图片上传成功";
HttpContext.Current.Cache.Remove(
"FTB-Images-" + this.CurrentImagesFolder);

return;
/*
if (this.inputFile.PostedFile != null && this.inputFile.PostedFile.FileName != null && !(IsAcceptedFileTypes(this.inputFile.PostedFile.FileName)))
{
this.returnMessage = "不允许上传该类型的文件";
return;
}
*/

}
base.RaisePostBackEvent(eventArgument);
}

private bool IsAcceptedFileTypes(string fileName)
{
for (int i = 0; i < this.AcceptedFileTypes.Length; i++)
{
if (fileName.ToLower().EndsWith("." + this.AcceptedFileTypes[i]))
{
return true;
}
}
return false;
}
}

 

 

 

 

posted @ 2010-08-31 10:50  builderman  阅读(954)  评论(5编辑  收藏  举报