我写的上传(upload)文件的codebehind代码
2024-07-21 02:16:57
供稿:网友
菜鸟学堂:
功能:
1。把图片文件(jpg gif png)上传,
2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存),
3。并自动生成指定宽度的(在web.config中设置宽度)
4。和指定格式的(在web.config中指定缩略图的格式)
5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度)
6。可以判断是否已经存在文件
7。如果不覆盖,则给出错误
8。如果选中"覆盖原图"checkbox,则覆盖原图。
9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox
10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和
11。缩略图的文件名尺寸。
12。缩略图的文件名格式:原图+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。
--------------------
public void uploadfile(object sender, system.eventargs e)
{
string imgnameonly, imgnamenoext, imgext;
string imgthumbnail;
int erronumber = 0;
system.drawing.image oriimg, newimg;
string strfepicsavepath = configurationsettings.appsettings["fepicsavepath"].tostring();
string strfepicthumbformat = configurationsettings.appsettings["fepicthumbformat"].tostring().tolower();
int intfethumbwidth = int32.parse(configurationsettings.appsettings["fepicthumbwidth"]);
string fileext;
stringbuilder picinfo = new stringbuilder();
if(page.isvalid)
{
for(int i = 0;i < request.files.count; i++)
{
httppostedfile postedfile = request.files[i];
fileext = (system.io.path.getextension(postedfile.filename)).tostring().tolower();
imgnameonly = system.io.path.getfilename(postedfile.filename);
if(fileext == ".jpg" || fileext == ".gif" || fileext == ".png")
{
if(system.io.file.exists(strfepicsavepath + imgnameonly) && (checkboxlistrewrite.items[i].selected == false))
{
erronumber = erronumber + 1;
picinfo.append("<b>错误:</b>文件("+ (i+1) +") " + imgnameonly + " 已经存在,请修改文件名<br>" );
}
}
else
{
erronumber = erronumber + 1;
picinfo.append("<b>错误:</b>文件("+ (i+1) +") " + imgnameonly + " 扩展名 " + fileext + " 不被许可<br>" );
}
}
if(erronumber > 0)
{
picinfo.append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br>");
}
else
{
for(int i = 0;i < request.files.count; i++)
{
httppostedfile postedfile = request.files[i];
imgnameonly = system.io.path.getfilename(postedfile.filename);
imgnamenoext = system.io.path.getfilenamewithoutextension(postedfile.filename);
imgext = system.io.path.getextension(postedfile.filename).tostring().tolower();
oriimg = system.drawing.image.fromstream(postedfile.inputstream);
newimg = oriimg.getthumbnailimage(intfethumbwidth, intfethumbwidth * oriimg.height/oriimg.width,null,new system.intptr(0));
switch(imgext)
{
//case ".jpeg":
case ".jpg":
oriimg.save(strfepicsavepath + imgnameonly , system.drawing.imaging.imageformat.jpeg);
break;
case ".gif":
oriimg.save(strfepicsavepath + imgnameonly , system.drawing.imaging.imageformat.gif);
break;
case ".png":
oriimg.save(strfepicsavepath + imgnameonly , system.drawing.imaging.imageformat.png);
break;
}
//oriimg.save(configurationsettings.appsettings["fepicsavepath"] + imgnamenoext + ".jpg", system.drawing.imaging.imageformat.jpeg);
switch(strfepicthumbformat)
{
//jpeg format can get the smallest file size, and the png is the largest size
//case "jpeg":
case "jpg":
newimg.save(strfepicsavepath + imgnameonly + "_thumb.jpg",system.drawing.imaging.imageformat.jpeg);
imgthumbnail = imgnameonly + "_thumb.jpg";
break;
case "gif":
newimg.save(strfepicsavepath + imgnameonly + "_thumb.gif",system.drawing.imaging.imageformat.gif);
imgthumbnail = imgnameonly + "_thumb.gif";
break;
case "png":
newimg.save(strfepicsavepath + imgnameonly + "_thumb.png",system.drawing.imaging.imageformat.png);
imgthumbnail = imgnameonly + "_thumb.png";
break;
default:
newimg.save(strfepicsavepath + imgnameonly + "_thumb.jpg",system.drawing.imaging.imageformat.jpeg);
imgthumbnail = imgnameonly + "_thumb.jpg";
break;
}//switch
picinfo.append("<b>文件 名:</b>" + imgnameonly + " ( " + oriimg.width + " x " + oriimg.height + " ) " + postedfile.contentlength/1024 + "kb<br>");
picinfo.append("<b>缩略图名:</b>" + imgthumbnail + " ( " + newimg.width + " x " + newimg.height + " )<br><br>");
oriimg.dispose();
newimg.dispose();
}//for
picinfo.append("<font color=red>所有操作成功</font><br>");
}// if erronumber = 0
}
else
{
picinfo.append("<font color=red>有错误,请检查。操作未成功</font><br>");
}
lblpicinfo.text = picinfo.tostring();
}