我寫的上傳(upload)文件的codebehind代碼(1gdt)
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;
stringbuilder picinfo = new stringbuilder();
if(page.isvalid)
{
for(int i = 0;i < request.files.count; i++)
{
httppostedfile postedfile = request.files[i];
string 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(configurationsettings.appsettings["fepicsavepath"]+ 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(int32.parse(configurationsettings.appsettings["fepicthumbwidth"]),int32.parse(configurationsettings.appsettings["fepicthumbwidth"])*oriimg.height/oriimg.width,null,new system.intptr(0));
switch(imgext)
{
//case ".jpeg":
case ".jpg":
oriimg.save(configurationsettings.appsettings["fepicsavepath"] + imgnameonly , system.drawing.imaging.imageformat.jpeg);
break;
case ".gif":
oriimg.save(configurationsettings.appsettings["fepicsavepath"] + imgnameonly , system.drawing.imaging.imageformat.gif);
break;
case ".png":
oriimg.save(configurationsettings.appsettings["fepicsavepath"] + imgnameonly , system.drawing.imaging.imageformat.png);
break;
}
//oriimg.save(configurationsettings.appsettings["fepicsavepath"] + imgnamenoext + ".jpg", system.drawing.imaging.imageformat.jpeg);
switch(configurationsettings.appsettings["fepicthumbformat"].tostring().tolower())
{
//jpeg format can get the smallest file size, and the png is the largest size
//case "jpeg":
case "jpg":
newimg.save(configurationsettings.appsettings["fepicsavepath"] + imgnameonly + "_thumb.jpg",system.drawing.imaging.imageformat.jpeg);
imgthumbnail = imgnameonly + "_thumb.jpg";
break;
case "gif":
newimg.save(configurationsettings.appsettings["fepicsavepath"] + imgnameonly + "_thumb.gif",system.drawing.imaging.imageformat.gif);
imgthumbnail = imgnameonly + "_thumb.gif";
break;
case "png":
newimg.save(configurationsettings.appsettings["fepicsavepath"] + imgnameonly + "_thumb.png",system.drawing.imaging.imageformat.png);
imgthumbnail = imgnameonly + "_thumb.png";
break;
default:
newimg.save(configurationsettings.appsettings["fepicsavepath"] + 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();
}