主頁(yè) > 知識(shí)庫(kù) > Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件實(shí)例

Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件實(shí)例

熱門標(biāo)簽:太原400電話上門辦理 浦發(fā)電話機(jī)器人提醒還款 百度地圖怎樣做地圖標(biāo)注 柳州電銷機(jī)器人公司 騰訊地圖標(biāo)注手機(jī) 征途美甲店地圖標(biāo)注 電銷語(yǔ)音機(jī)器人型號(hào)參數(shù) 400電話如何申請(qǐng)取消 昆明語(yǔ)音電銷機(jī)器人價(jià)格

本文實(shí)例講述了Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件的方法。分享給大家供大家參考。

具體功能代碼如下:

復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CSFramework.BLL
{
   /// summary>
   /// 支持上傳的文件類型
   /// /summary>
   public enum UploadFileType
   {
      ArticleAttachment = 1,
      Image = 2,
      Video = 3,
      All = 4
   }
  
   /// summary>
   /// 上傳文件管理類
   /// /summary>
   public class CFileUpload
   {
      private FileUpload _fileUpload;
      private string _savePath;
      private string _LastUploadedFile = string.Empty;
      private bool _AutoGenFileName = false;
      private bool _AutoGenWatermark = false;
      public string LastUploadedFile { get { return _LastUploadedFile; } }
      private string _Error = "";
     
      private string PICTURE_FILE = "[.gif.png.jpeg.jpg]";
      private string ZIP_FILE = "[.zip.rar]";
      private string MUILT_MEDIA_FILE = "[.mpeg.mpg.fla.wma]";
     
      private int IMG_MAX_WIDTH = 700;//指定寬度
      private int IMG_MAX_HEIGHT = 0;//未指定高度
      private int MAX_SIZE_UPLOAD = 1024;//最大支持上傳小于1MB的文件。
     
      /// summary>
      /// 構(gòu)造器
      /// /summary>
      /// param name="fileUpload">Asp.net FileUpload對(duì)象/param>
      /// param name="savePath">保存目錄,不包含文件名/param>
      /// param name="autoGenFileName">自動(dòng)生成文件名/param>
      public CFileUpload(FileUpload fileUpload, string savePath, bool autoGenFileName, bool autoGenWatermark)
      {
         _savePath = savePath;
         _fileUpload = fileUpload;
         _AutoGenFileName = autoGenFileName;
         _AutoGenWatermark = autoGenWatermark;
      }
     
      /// summary>
      /// 構(gòu)造器
      /// /summary>
      /// param name="fileUpload">Asp.net FileUpload對(duì)象/param>
      /// param name="savePath">保存目錄,不包含文件名/param>
      public CFileUpload(FileUpload fileUpload, string savePath)
      {
         _savePath = savePath;
         _fileUpload = fileUpload;
      }
     
      /// summary>
      /// 上傳RAR文件
      /// /summary>
      public bool UploadRARFile()
      {
         return DoUpload(ZIP_FILE);
      }
     
      /// summary>
      /// 上傳視頻文件
      /// /summary>
      public bool UploadVideo()
      {
         return DoUpload(MUILT_MEDIA_FILE);
      }
     
      /// summary>
      /// 上傳圖片文件
      /// /summary>
      public bool UploadImage()
      {
         return DoUpload(PICTURE_FILE);
      }
     
      public bool UploadImage(int maxWidth, int maxHeight)
      {
         this.IMG_MAX_WIDTH = maxWidth;
         this.IMG_MAX_HEIGHT = maxHeight;
         return DoUpload(PICTURE_FILE);
      }
     
      /// summary>
      /// 上傳任何支持的文件
      /// /summary>
      public bool UploadAnySupported()
      {
         return DoUpload(PICTURE_FILE ZIP_FILE MUILT_MEDIA_FILE);
      }
     
      /// summary>
      /// 生成新的文件名
      /// /summary>
      private string GetNewFileName(string folder, string fileName)
      {
         if (_AutoGenFileName) //自動(dòng)生成32位GUID文件名
         {
            string ext = System.IO.Path.GetExtension(fileName);
            string newfile = Guid.NewGuid().ToString().Replace("-", "") ext;
            return folder newfile;
         }
         else
         {
            if (System.IO.File.Exists(folder fileName))
            {
               string ext = System.IO.Path.GetExtension(fileName);
               string filebody = fileName.Replace(ext, "");
              
               int x = 1;
               while (true) //如果文件存在,生成尾部帶(x)的文件
               {
                  string newfile = folder filebody "(" x.ToString() ")" ext;
                  if (!System.IO.File.Exists(newfile))
                  return folder filebody "(" x.ToString() ")" ext;
                  else
                  x ;
               }
            }
            else
            return folder fileName;
         }
      }
     
      /// summary>
      /// 最大支持小于1MB的文件。
      /// /summary>
      private bool AllowMaxSize(int fileLength)
      {
         double kb = fileLength / 1024;
         return (int)kb MAX_SIZE_UPLOAD;
      }
     
      private bool DoUpload(string allowedExtensions)
      {
         try
         {
            bool fileOK = false;
           
            if (!_fileUpload.HasFile) throw new Exception("沒(méi)有文件!"); //上傳控件中如果不包含文件,退出
           
            // 得到文件的后綴
            string fileExtension = System.IO.Path.GetExtension(_fileUpload.FileName).ToLower();
           
            // 看包含的文件是否是被允許的文件后綴
            fileOK = allowedExtensions.IndexOf(fileExtension) > 0;
            if (!fileOK) throw new Exception("不支持的文件格式!");
           
            //檢查上傳文件大小
            fileOK = AllowMaxSize(_fileUpload.FileBytes.Length);
            if (!fileOK) throw new Exception("圖片文件不能大于" MAX_SIZE_UPLOAD.ToString() "KB!");
           
            try
            {
               // 文件另存在服務(wù)器指定目錄下
               string savefile = GetNewFileName(_savePath, _fileUpload.FileName);
              
               if (IsUploadImage(fileExtension))//保存圖片
               {
                  System.Drawing.Image output = CImageLibrary.FromBytes(_fileUpload.FileBytes);
                 
                  // 檢查圖片寬度/高度/大小
                  if (this.IMG_MAX_WIDTH != 0 output.Width > this.IMG_MAX_WIDTH)
                  {
                     output = CImageLibrary.GetOutputSizeImage(output, this.IMG_MAX_WIDTH);
                  }
                 
                  Bitmap bmp = new Bitmap(output);
                 
                  this.CreateDir(Path.GetDirectoryName(savefile));
                 
                  bmp.Save(savefile, output.RawFormat);
                  bmp.Dispose();
                  output.Dispose();
                 
                  if (_AutoGenWatermark)
                  {
                     WatermarkImage genWatermark = new WatermarkImage();
                     genWatermark.DrawWords(savefile, AppConfig.Current.WatermarkMain,
                     AppConfig.Current.WatermarkDesc, float.Parse("0.2"));
                  }
               }
               else//其它任何文件
               {
                  this.CreateDir(Path.GetDirectoryName(savefile));
                 
                  _fileUpload.PostedFile.SaveAs(savefile);
               }
              
               _LastUploadedFile = savefile;
              
               return true;
            }
            catch (Exception ex)
            {
               throw new Exception("上傳文件時(shí)發(fā)生未知錯(cuò)誤!" ex.Message);
            }
         }
         catch (Exception ex)
         {
            _Error = ex.Message;
            return false;
         }
      }
     
      private void CreateDir(string dir)
      {
         if (Directory.Exists(dir) == false)
         Directory.CreateDirectory(dir);
      }
     
      private bool IsUploadImage(string fileExtension)
      {
         bool isImage = PICTURE_FILE.IndexOf(fileExtension) > 0;
         return isImage;
      }
   }
}

希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • Asp.Net修改上傳文件大小限制方法
  • Asp.net MVC中使用JQuery插件ajaxFileUpload上傳文件
  • ASP.NET插件uploadify批量上傳文件完整使用教程
  • asp.net上傳文件到數(shù)據(jù)庫(kù)的解決方案
  • asp.net中MVC借助Iframe實(shí)現(xiàn)無(wú)刷新上傳文件實(shí)例
  • asp.net fileupload控件上傳文件與多文件上傳
  • Asp.Net模擬表單提交數(shù)據(jù)和上傳文件的實(shí)現(xiàn)代碼
  • asp.net實(shí)現(xiàn)上傳文件顯示本地絕對(duì)路徑的實(shí)例代碼
  • 如何限制asp.net中上傳文件的大小的代碼
  • asp.net 模擬提交有文件上傳的表單(通過(guò)http模擬上傳文件)
  • 解決.net項(xiàng)目中上傳的圖片或者文件太大無(wú)法上傳問(wèn)題

標(biāo)簽:新疆 陽(yáng)泉 蘭州 德陽(yáng) 白山 張家界 天門 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件實(shí)例》,本文關(guān)鍵詞  Asp.Net,的,FileUpload,類,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件實(shí)例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章