我寫的上傳(upload)文件的codebehind代碼
2024-07-21 02:16:57
供稿:網(wǎng)友
菜鳥學(xué)堂:
功能:
1。把圖片文件(jpg gif png)上傳,
2。保存到指定的路徑(在web.config中設(shè)置路徑,以文件的原有格式保存),
3。并自動(dòng)生成指定寬度的(在web.config中設(shè)置寬度)
4。和指定格式的(在web.config中指定縮略圖的格式)
5。和原圖比例相同的縮略圖(根據(jù)寬度和原圖的寬和高計(jì)算所略圖的高度)
6。可以判斷是否已經(jīng)存在文件
7。如果不覆蓋,則給出錯(cuò)誤
8。如果選中"覆蓋原圖"checkbox,則覆蓋原圖。
9。可以根據(jù)要求,在webform上設(shè)置1個(gè)以上的file input和相應(yīng)的checkbox
10。并在文件上傳完畢后,顯示原圖的文件名,尺寸,字節(jié),和
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>錯(cuò)誤:</b>文件("+ (i+1) +") " + imgnameonly + " 已經(jīng)存在,請(qǐng)修改文件名<br>" );
}
}
else
{
erronumber = erronumber + 1;
picinfo.append("<b>錯(cuò)誤:</b>文件("+ (i+1) +") " + imgnameonly + " 擴(kuò)展名 " + fileext + " 不被許可<br>" );
}
}
if(erronumber > 0)
{
picinfo.append("<font color=red>全部操作均未完成,請(qǐng)修改錯(cuò)誤,再進(jìn)行操作</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>有錯(cuò)誤,請(qǐng)檢查。操作未成功</font><br>");
}
lblpicinfo.text = picinfo.tostring();
}