最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#多圖片上傳并生成縮略圖的實例代碼

 更新時間:2013年04月15日 21:56:58   作者:  
今天寫了一個上傳多張圖片并生成縮略圖的小程序。當然因為是菜鳥,所以寫的一般。但還是學到了不少東西?,F(xiàn)在上代碼。

前臺代碼:

復制代碼 代碼如下:


 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title></title>
     <style type="text/css">
         li
         {
             list-style: none;
             padding-top: 10px;
         }
     </style>
     <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
     <script type="text/javascript">
         function ValidImage(id, msg) {
             $(id).parent().append("<span>" + msg + "</span>");
             return false;
         }
     </script>
 </head>
 <body>
     <form id="form1" runat="server" enctype="multipart/form-data" method="post">
         <div>
                       <ul>
                 <li>
                     <input type="file" id="upload1" name="upload" />
                 </li>
                 <li>
                     <input type="file" id="upload2" name="upload" />
                 </li>
                 <li>
                     <input type="file" id="upload3" name="upload" />
                 </li>
                 <li>
                     <input type="file" id="upload4" name="upload" /></li>
                 <li>
                     <input type="file" id="upload5" name="upload" />

                 </li>
                 <li>
                     <input type="submit" id="btnPostFile" runat="server" onserverclick="btnPostFile_ServerClick" value="開始上傳" />
                 </li>
             </ul>
         </div>
     </form>
 </body>
 </html>

前臺就是幾個控件和一個ValidImage方法。

后臺代碼:

復制代碼 代碼如下:


  protected void btnPostFile_ServerClick(object sender, EventArgs e)
     {
         string filePath = Server.MapPath("/uploadImg");
         const int size = 5242880;
         if (!Directory.Exists(filePath))
         {
             Directory.CreateDirectory(filePath);
         }
         if (Request.Files.Count > 0)
         {
             for (int i = 0; i < Request.Files.Count; i++)
             {
                 HttpPostedFile postFile = Request.Files[i];
                 string uploadFileID = string.Format("#upload{0}", i + 1);  //當前的上傳控件ID,因為jquery要調(diào)用就加了#
                 string msg = null;                 //提示信息
                 if (postFile.FileName.Trim().Length <= 0)
                 {
                     continue;
                 }
                 if (postFile.ContentLength > size)
                 {
                     msg = "文件太大";
                     Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//將提示信息發(fā)送到客戶端
                     continue;
                 }
                 string savePath = Path.Combine(filePath, postFile.FileName);        //圖片的保存地址
                 if (!File.Exists(savePath))
                 {
                     postFile.SaveAs(Path.Combine(filePath, postFile.FileName));     //如果文件不存在就保存
                 }
                 else
                 {
                     msg = "文件" + postFile.FileName + "已經(jīng)存在";
                     Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//將提示信息發(fā)送到客戶端
                     continue;
                 }
                 if (IsImg(savePath))            //通過IsImg方法驗證文件是否是圖片,或者格式是否正確
                 {
                     SmallImg(postFile.InputStream, postFile.FileName);
                 }
                 else
                 {
                     msg = "只能上傳JGP、PNG類型的圖片,請檢查文件格式是否正確";
                     Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//將提示信息發(fā)送到客戶端
                     File.Delete(savePath);  //如果不是圖片就刪除
                 }
             }
         }
     }

復制代碼 代碼如下:

  #region 驗證上傳文件的格式
     /// <summary>
     /// 驗證上傳文件是否是圖片
     /// </summary>
     /// <param name="FilePath">文件的保存路徑</param>
     /// <returns></returns>
     private bool IsImg(string FilePath)
     {
         using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
         {
             bool result = false;
             BinaryReader br = new BinaryReader(fs, System.Text.Encoding.UTF8);
             string strImg = "";
             byte buffer;
             try
             {
                 buffer = br.ReadByte();
                 strImg = buffer.ToString();
                 buffer = br.ReadByte();
                 strImg += buffer.ToString();
             }
             catch
             {
                 fs.Close();
                 br.Close();

             }
             if (strImg == "255216" || strImg == "13780")//說明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
             {
                 result = true;
             }
             return result;
         }
     }
     #endregion

復制代碼 代碼如下:


   #region 將圖片生成縮略圖
     /// <summary>
     /// 生成縮略圖
     /// </summary>
     private void SmallImg(Stream oStream, string FileName)
     {
         using (System.Drawing.Image img = System.Drawing.Image.FromStream(oStream))
         {
             int newWidth = 100;
             int newHeight = 80;
             int oldWidth = img.Width;
             int oldHeight = img.Height;
             if (oldWidth > oldHeight)
             {
                 newHeight = (int)Math.Floor((double)oldHeight * (double)newWidth / (double)oldWidth);
             }
             else
             {
                 newWidth = (int)Math.Floor((double)oldWidth * (double)newHeight / (double)oldHeight);
             }
             using (Bitmap bmp = new Bitmap(newWidth, newHeight))
             {
                 using (Graphics g = Graphics.FromImage(bmp))
                 {
                     g.Clear(Color.Transparent);
                     g.InterpolationMode = InterpolationMode.High;
                     g.CompositingQuality = CompositingQuality.HighQuality;
                     g.SmoothingMode = SmoothingMode.HighQuality;
                     g.DrawImage(img, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, oldWidth, oldHeight), GraphicsUnit.Pixel);
                     string newFileName = Path.GetFileNameWithoutExtension(FileName) + "_small" + Path.GetExtension(FileName);   //縮略圖名稱
                     string filePath = Server.MapPath("/uploadImg/") + newFileName;
                     bmp.Save(filePath);
                 }
             }

         }
     }
     #endregion

代碼有很多需要改進的地方,希望大家多多指點。

相關(guān)文章

  • asp.net下PageMethods使用技巧

    asp.net下PageMethods使用技巧

    ASP.net AjAX中的PageMethods可以將靜態(tài)頁方法添加到 ASP.NET 頁中并將其用作 Web 方法。然后,無需創(chuàng)建單獨的 .asmx 文件即可從該頁中的腳本調(diào)用這些方法,就好像這些方法是 Web 服務的一部分。特別是在一些交互流程不復雜而調(diào)用次數(shù)和方法又比較多的情況下更為方便。因為PageMethods不需要我們再添加另外的WEB服務或Page來處理請求。
    2008-03-03
  • asp.net使用jQuery Uploadify上傳附件示例

    asp.net使用jQuery Uploadify上傳附件示例

    Uploadify是JQuery的一個上傳插件,實現(xiàn)的效果非常不錯,帶進度顯示,本文是一個簡單的介紹Demo,主要是動態(tài)傳遞參數(shù)方法,通過formdata 向處理程序傳遞額外的表單數(shù)據(jù)
    2014-01-01
  • ASP.NET簡化編輯界面解決思路及實現(xiàn)代碼(2)

    ASP.NET簡化編輯界面解決思路及實現(xiàn)代碼(2)

    這篇與前一篇改進部分,也許大家會留意到動畫演示,主要是GridVeiw的更新與刪除會在每row都有。因此Insus.NET把它抽取出來,放在GridView外,感興趣的朋友可以了解下啊,希望本文對你有所幫助
    2013-01-01
  • .NET MVC中ViewData,ViewBag和TempData的區(qū)別淺析

    .NET MVC中ViewData,ViewBag和TempData的區(qū)別淺析

    這篇文章主要介紹了.NET MVC中ViewData,ViewBag和TempData的區(qū)別,分析了ViewData,ViewBag和TempData在賦值、功能特性等方面的區(qū)別于用法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • asp.net+ajax的Post請求實例

    asp.net+ajax的Post請求實例

    這篇文章主要介紹了asp.net+ajax的Post請求實現(xiàn)方法,實例分析了Ajax的發(fā)送post數(shù)據(jù)的原理與技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • asp.net Application_AcquireRequestState事件,導致Ajax客戶端不能加載

    asp.net Application_AcquireRequestState事件,導致Ajax客戶端不能加載

    項目中使用Application_AcquireRequestState事件,來做一些用戶信息的驗證工作.
    2010-03-03
  • .net中 發(fā)送郵件內(nèi)容嵌入圖片的具體實例

    .net中 發(fā)送郵件內(nèi)容嵌入圖片的具體實例

    這篇文章主要介紹了.net中 發(fā)送郵件內(nèi)容嵌入圖片的具體實例,需要的朋友可以參考下
    2014-02-02
  • 利用ASP.NET MVC和Bootstrap快速搭建響應式個人博客站(一)

    利用ASP.NET MVC和Bootstrap快速搭建響應式個人博客站(一)

    這篇文章主要介紹了利用ASP.NET MVC和Bootstrap快速搭建響應式個人博客站(一)的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • .NET微信公眾號查看關(guān)注者接口

    .NET微信公眾號查看關(guān)注者接口

    這篇文章主要為大家詳細介紹了.NET微信公眾號查看關(guān)注者接口的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • asp.net String.Empty NULL 不同之處

    asp.net String.Empty NULL 不同之處

    在asp.net(c#)中String.Empty、NULL、"" 3個語法經(jīng)常使用,作用是判斷字符串是否為空。
    2009-06-06

最新評論

内黄县| 四子王旗| 鄯善县| 嘉义县| 宝清县| 昌吉市| 乳山市| 浮梁县| 津南区| 闸北区| 津南区| 德阳市| 甘德县| 搜索| 常德市| 濮阳市| 财经| 全州县| 金塔县| 湘阴县| 永仁县| 额济纳旗| 乌海市| 行唐县| 上栗县| 泗水县| 平远县| 光泽县| 织金县| 乌兰县| 威海市| 北宁市| 文成县| 大关县| 乌苏市| 马关县| 枣阳市| 于都县| 扎鲁特旗| 武冈市| 凌海市|