我写的上传(upload)文件的codebehind代码(1gdt)
来源:互联网 发布:法国潜艇机密数据 下载 编辑:程序博客网 时间:2024/06/06 07:43
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
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();
&nb
我写的上传(upload)文件的codebehind代码(1gdt)';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
功能: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();
&nb
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 我写的上传(upload)文件的codebehind代码(1gdt)
- 我写的上传(upload)文件的codebehind代码
- Python写的一个文件上传upload
- upload文件的上传
- com.css.common.upload.servlet关于文件上传的代码
- commons-upload 实现文件的上传功能
- ThinkPHP 文件上传Upload类的使用
- Nginx+upload+lua 简单的文件上传
- bootstrap upload 文件上传的实现过程
- 基于Java Socket的文件UpLoad代码
- 基于Java Socket的文件UpLoad代码
- 基于Java Socket的文件UpLoad代码
- 基于Java Socket的文件UpLoad代码
- 基于Java Socket的文件UpLoad代码
- 基于Java Socket的文件UpLoad代码
- 基于Java Socket的文件UpLoad代码
- 基于Java Socket的文件UpLoad代码
- 基于Java Socket的文件UpLoad代码
- 生成sessionid和随机密码的例子
- page_Load和page_Init的区别
- php生成EXCEL的东东
- 使用C#开发用户控制(转)
- PHP中对数据库操作的封装
- 我写的上传(upload)文件的codebehind代码(1gdt)
- 一个取得文件扩展名的函数
- C# 积木模块 ABC(二)在C#中部署应用程序
- 用ODBC的分页显示
- ASP.NET中异常处理使用(详细)
- 为查询结果建立向后/向前按钮
- ASP.NET Framework深度历险(1)
- 利用PHP实现与ASP Banner组件相似的类
- 【教程】【转载】C#锐利体验(李建忠)—03