真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

實(shí)現(xiàn)HttpPostedFileBase文件上傳的方法-創(chuàng)新互聯(lián)

小編給大家分享一下實(shí)現(xiàn)HttpPostedFileBase文件上傳的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)與策劃設(shè)計(jì),鎮(zhèn)平網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:鎮(zhèn)平等地區(qū)。鎮(zhèn)平做網(wǎng)站價(jià)格咨詢:028-86922220

HttpPostedFileBase文件上傳,支持多文件一次上傳,如有圖片,則支持略縮圖保存

文件傳輸信息封裝

/// 
  /// 文件生成方式
  /// 
  public class UpFileMessage
  {
    /// 
    /// 文件名
    /// 
    public string OriginalFileName { get; set; }

    /// 
    /// 是否保存略縮圖
    /// 
    public bool IsImage { get; set; }

    /// 
    /// 文件流
    /// 
    public Stream FileStream { get; set; }

    /// 
    /// 生成縮略圖的方式
    /// [WH]-指定寬高
    /// [H]-指定高,按比例縮放寬
    /// [W]-指定寬,按比例縮放高
    /// 
    public string Mode { get; set; }

    /// 
    /// 略縮圖高度
    /// 
    public int? ThumbHeight { get; set; }

    /// 
    /// 略縮圖寬度
    /// 
    public int? ThumbWidth { get; set; }

  }

文件上傳返回結(jié)果

/// 
  /// 文件上傳返回結(jié)果
  /// 
  public class UpFileResultMessage
  {
    /// 
    /// 文件保存是否成功
    /// 
    public bool IsSuccess { get; set; }

    /// 
    /// 錯(cuò)誤消息
    /// 
    public string Message { get; set; }

    /// 
    /// 原始文件名-(無(wú)擴(kuò)展名)
    /// 
    public string FileName { get; set; }

    /// 
    /// 文件名擴(kuò)展名
    /// 
    public string FileSuffix { get; set; }

    /// 
    /// 文件名保存路徑
    /// 
    public string FilePath { get; set; }

    /// 
    /// 文件類型為圖片時(shí)
    /// 縮略圖保存路徑
    /// 
    public string ThumbPath { get; set; }

    /// 
    /// 保存文件名(無(wú)擴(kuò)展名)
    /// 
    public string SaveFileName { get; set; }

    /// 
    /// 文件自增ID
    /// 
    public int[] FileIdArray { get; set; }
  }

文件上傳類庫(kù)

需引用System.Web命名空間,并對(duì) [System.Web.UI.Page] 進(jìn)行繼承,調(diào)用Server.MapPath方法

public class FileUtil : System.Web.UI.Page
  {
    /// 
    /// 圖片上傳
    /// 
    /// 文件生成方式
    /// 
    public UpFileResultMessage UpLoadFile(UpFileMessage fileMessage)
    {
      try
      {
        string now = DateTime.Today.ToString("yyyyMMdd");
        string guid = Guid.NewGuid().ToString();

        //獲取文件擴(kuò)展名
        var fileSuffix = Path.GetExtension(fileMessage.OriginalFileName);

        var uploadFolder = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(ParmsConfig.UpFilePathUrl), now);

        if (!Directory.Exists(uploadFolder))
        {
          Directory.CreateDirectory(uploadFolder);
        }

        //保存文件名
        string saveFileName = guid + fileSuffix;
        string filePath = Path.Combine(uploadFolder, saveFileName);


        UpFileResultMessage upFileResult = new UpFileResultMessage()
        {
          IsSuccess = true,
          FileName = Path.GetFileNameWithoutExtension(fileMessage.OriginalFileName),
          FileSuffix = fileSuffix,
          FilePath = string.Format(@"{0}/{1}", ParmsConfig.UpFileIPAddressUrl, now),
          SaveFileName = guid
        };

        using (Stream sourceStream = fileMessage.FileStream)
        {
          using (FileStream targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
          {
            const int bufferLen = 1024 * 4;//4KB
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
            {
              targetStream.Write(buffer, 0, count);
            }
          }
          //上傳文件為圖片時(shí),需創(chuàng)建縮略圖
          if (fileMessage.IsImage)
          {
            var uploadThumbFolder = Path.Combine(uploadFolder, "Thumb");

            if (!Directory.Exists(uploadThumbFolder))
            {
              Directory.CreateDirectory(uploadThumbFolder);
            }
            using (FileStream targetStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
            {
              System.Drawing.Image image = System.Drawing.Image.FromStream(targetStream);
              int width = image.Width;
              int height = image.Height;
              int thumbWidth = 0;
              int thumbHeight = 0;
              switch (fileMessage.Mode)
              {
                case "WH": //指定高寬縮放(可能變形) 
                  thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;
                  thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200;
                  break;
                case "W":  //指定寬,高按比例   
                  thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;
                  thumbHeight = height * thumbWidth / width;
                  break;
                case "H":  //指定高,寬按比例
                  thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200;
                  thumbWidth = width * thumbHeight / height;
                  break;
                default:
                  thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;
                  thumbHeight = height * thumbWidth / width;
                  break;
              }
              string thumbFilePath = Path.Combine(uploadThumbFolder, saveFileName);
              CreateThumbnail(thumbFilePath, targetStream, thumbWidth, thumbHeight);
              upFileResult.ThumbPath = string.Format(@"{0}/{1}/Thumb", ParmsConfig.UpFileIPAddressUrl, now);
            }
          }
        }

        return upFileResult;
      }
      catch (Exception ex)
      {
        return new UpFileResultMessage() { IsSuccess = false, Message = ex.Message };
      }

    }

    /// 
    /// 創(chuàng)建指定圖片文件流的縮略圖
    /// 
    /// 縮略圖文件保存路徑
    /// 原始文件流
    /// 縮略圖寬
    /// 縮略圖高
    private void CreateThumbnail(string thumbFilePath, Stream fileStream, int width, int height)
    {
      using (Image image = Image.FromStream(fileStream))
      {
        using (Image thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero))
        {
          thumbnail.Save(thumbFilePath);
        }
      }

    }

  }

調(diào)用方式

var upFileMsg = new UpFileMessage()
          {
            IsImage = true,
            OriginalFileName = platformImg[i].FileName,
            FileStream = platformImg[i].InputStream,
            ThumbWidth = ThumbWidth,
            Mode = "W"
          };
         var  upFileResultMsg = new FileUtil().UpLoadFile(upFileMsg);

以上是“實(shí)現(xiàn)HttpPostedFileBase文件上傳的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


網(wǎng)頁(yè)名稱:實(shí)現(xiàn)HttpPostedFileBase文件上傳的方法-創(chuàng)新互聯(lián)
文章分享:http://weahome.cn/article/dhjceh.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部