首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 笔记本电脑 | 北大青鸟 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> ASP.NET >> 正文

通过.NET上传图象

    以前,通过ASP上传图象(图象的大小、类型都受到限制)一般都是要借助外部组件来完成,.NET的出现,使这一工作变得非常容易并且可以随便的使用Bitmap和Image类型。

    在这个指导思想下,我将按照以下步骤(在你要上传图象文件上)创建一个简单的WEB窗体,该窗体将判断上传的文件是否是JPEG文件、判断该文件是否存在(必要时你可以重命名)。

    1、  创建一个新Web 应用程序项目;

    2、  打开Web 窗体;

    3、  在窗体上面添加一个HTML表单,并把它转换成服务器控件。在这个例子里,该文件将命名为filUpload;(把HTML转换成服务器控件的方法是,在它的上面右击鼠标然后选择Run As Server Control)

    4、  切换到HTML view并添加/更改FORM标签的enctype属性为multipart/form-data。如:enctype="multipart/form-data"。

    5、  在Web窗体上添加一个BUTTON并命名为btnUpload。

    6、  向Web应用程序添加一个folder called /images。

    7、  在窗体上添加一个Web Form Image并命名为imgPicture,设置宽度和高度分别为160和120。

    8、  添加一个Label控件并命名为lblOutput。显示当在上传的过程中发生的任何错误。

    9、  给按钮btnUpload的单击事件添加如下代码:(如果你想分析以下代码的细节,你可以把下面的代码复制粘贴到VS.NET IDE集成开发环境。)

    1.     private void btnUpload_Click(object sender, System.EventArgs e)
    2.     {
    3.         // Initialize variables
    4.         string sSavePath;
    5.         string sThumbExtension;
    6.         int intThumbWidth;
    7.         int intThumbHeight;
    8.
    9.         // Set constant values
    10.      sSavePath = "images/";
    11.      sThumbExtension = "_thumb";
    12.      intThumbWidth = 160;
    13.      intThumbHeight = 120;
    14.
    15.      // If file field isn’t empty
    16.      if (filUpload.PostedFile != null)
    17.      {
    18.          // Check file size (mustn’t be 0)
    19.          HttpPostedFile myFile = filUpload.PostedFile;
    20.          int nFileLen = myFile.ContentLength;
    21.          if (nFileLen == 0)
    22.          {
    23.              lblOutput.Text = "No file was uploaded.";
    24.              return;
    25.          }
    26.
    27.          // Check file extension (must be JPG)
    28.          if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
    29.          {
    30.              lblOutput.Text = "The file must have an extension of JPG";
    31.              return;
    32.          }
    33.
    34.          // Read file into a data stream
    35.          byte[] myData = new Byte[nFileLen];
    36.          myFile.InputStream.Read(myData,0,nFileLen);
    37.
    38.          // Make sure a duplicate file doesn’t exist.  If it does, keep on appending an
    39.          // incremental numeric until it is unique
    40.          string sFilename = System.IO.Path.GetFileName(myFile.FileName);
    41.          int file_append = 0;
    42.          while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
    43.          {
    44.              file_append++;
    45.              sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
    46.                               + file_append.ToString() + ".jpg";
    47.          }
    48.
    49.          // Save the stream to disk
    50.          System.IO.FileStream newFile
    51.                  = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename),
    52.                                             System.IO.FileMode.Create);
    53.          newFile.Write(myData,0, myData.Length);
    54.          newFile.Close();
    55.
    56.          // Check whether the file is really a JPEG by opening it
    57.          System.Drawing.Image.GetThumbnailImageAbort myCallBack =
    58.                         new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
    59.          Bitmap myBitmap;
    60.          try
    61.          {
    62.              myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename));
    63.
    64.              // If jpg file is a jpeg, create a thumbnail filename that is unique.
    65.              file_append = 0;
    66.              string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
    67.                                                       + sThumbExtension + ".jpg";
    68.              while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
    69.              {
    70.                  file_append++;
    71.                  sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
    72.                                 file_append.ToString() + sThumbExtension + ".jpg";
    73.              }
    74.
    75.              // Save thumbnail and output it onto the webpage
    76.              System.Drawing.Image myThumbnail
    77.                      = myBitmap.GetThumbnailImage(intThumbWidth,
    78.                                                   intThumbHeight, myCallBack, IntPtr.Zero);
    79.              myThumbnail.Save (Server.MapPath(sSavePath + sThumbFile));
    80.              imgPicture.ImageUrl = sSavePath + sThumbFile;
    81.
    82.              // Displaying success information
    83.              lblOutput.Text = "File uploaded successfully!";
    84.
    85.              // Destroy objects
    86.              myThumbnail.Dispose();
    87.              myBitmap.Dispose();
    88.          }
    89.          catch (ArgumentException errArgument)
    90.          {
    91.              // The file wasn't a valid jpg file
    92.              lblOutput.Text = "The file wasn't a valid jpg file.";
    93.              System.IO.File.Delete(Server.MapPath(sSavePath + sFilename));
    94.          }
    95.      }
    96.  }
    97.
    98.  public bool ThumbnailCallback()
    99.  {
    100.         return false;
    }

    10.运行以上创建的 Web页(webpage),并分别使用JPG文件和其他类型的文件来测试错误检查(error-checking)机制。

【责编:Amy】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·开源软件测试工具学习专题
 ·JSP Web开发 入门基础到高手进阶教程…
 ·JavaFX—是Java桌面的新希望么?
 ·安全至上 .NET开发安全策略…
 ·测试用例设计之道-测试用例学习专题
 ·面向Java开发人员的Scala指南
 ·Java设计模式之实例详解
 ·Oracle数据库11g 面向DBA和开发人员的重要新特性…
 ·桌面应用软件编程 J2SE技术详解…
 ·我“炫”我精彩-------WPF开发教程
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航