c#多图片上传并生成缩略图的实例代码

复制代码 代码如下:



 <%@ 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="https://www.jb51.net/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 runat="server" enctype="multipart/form-data" method="post">
         <div>
                       <ul>
                 <li>
                     <input type="file" />
                 </li>
                 <li>
                     <input type="file" />
                 </li>
                 <li>
                     <input type="file" />
                 </li>
                 <li>
                     <input type="file" /></li>
                 <li>
                     <input type="file" />

                 </li>
                 <li>
                     <input type="submit" 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要调用就加了#
                 string msg = null;                 //提示信息
                 if (postFile.FileName.Trim().Length <= 0)
                 {
                     continue;
                 }
                 if (postFile.ContentLength > size)
                 {
                     msg = "文件太大";
                     Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//将提示信息发送到客户端
                     continue;
                 }
                 string savePath = Path.Combine(filePath, postFile.FileName);        //图片的保存地址
                 if (!File.Exists(savePath))
                 {
                     postFile.SaveAs(Path.Combine(filePath, postFile.FileName));     //如果文件不存在就保存
                 }
                 else
                 {
                     msg = "文件" + postFile.FileName + "已经存在";
                     Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//将提示信息发送到客户端
                     continue;
                 }
                 if (IsImg(savePath))            //通过IsImg方法验证文件是否是图片,或者格式是否正确
                 {
                     SmallImg(postFile.InputStream, postFile.FileName);
                 }
                 else
                 {
                     msg = "只能上传JGP、PNG类型的图片,请检查文件格式是否正确";
                     Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//将提示信息发送到客户端
                     File.Delete(savePath);  //如果不是图片就删除
                 }
             }
         }
     }

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgsgw.html