惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
A
Arctic Wolf
Security Latest
Security Latest
T
Threatpost
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
G
GRAHAM CLULEY
The Hacker News
The Hacker News
C
CERT Recently Published Vulnerability Notes
Know Your Adversary
Know Your Adversary
I
Intezer
Scott Helme
Scott Helme
T
Tenable Blog
NISL@THU
NISL@THU
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
N
News and Events Feed by Topic
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Project Zero
Project Zero
Latest news
Latest news
Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Forbes - Security
Forbes - Security
Security Archives - TechRepublic
Security Archives - TechRepublic
AI
AI
S
Security Affairs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
D
Docker
P
Proofpoint News Feed
博客园 - Franky

博客园 - goooto

Spring4.1新特性——Spring缓存框架增强(转) Spring整合Ehcache管理缓存(转) 《权力的游戏》第七季片场照曝光 MINA2 框架详解(转) Docker简明教程(转) IIS7.5 Gzip压缩配置 UML 依赖 关联 聚合 组合 亲属称谓 C#命名规范(微软官方版) MongoDB学习笔记(六) MongoDB索引用法和效率分析(转) MongoDB学习笔记(五) MongoDB文件存取操作(转) MongoDB学习笔记(四) 用MongoDB的文档结构描述数据关系(转) MongoDB学习笔记(三) 在MVC模式下通过Jqgrid表格操作MongoDB数据(转) MongoDB学习笔记(二) 通过samus驱动实现基本数据操作(转) MongoDB学习笔记(一)MongoDB介绍及安装(转) Log4net配置相关 VS2010快捷键大全 JQuery中得到Element真实top、left、height和width属性值的对象 - goooto - 博客园 WindowsService注册
以字节流上传文件
goooto · 2011-11-20 · via 博客园 - goooto

public string SaveFile(byte[] binData, string filePath, string fileName, int type, string fileType)
       {

           ///定义并实例化一个内存流,以存放提交上来的字节数组。
           MemoryStream m = new MemoryStream(binData);
           FileStream fileStream = null;
           try
           {
               string savePath = System.Configuration.ConfigurationSettings.AppSettings["DcFilePath"];

               //不是绝对路径 
               if (savePath.IndexOf(":\\") < 0) savePath = Server.MapPath(savePath);

               //没有反斜杠
               if (!savePath.EndsWith("\\")) savePath += "\\";

               //目录
               savePath += filePath;

               //没有文件夹,创建文件夹

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

               if (type == 0)
               {
                   fileName = "\\" + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
               }
               if (!File.Exists(savePath + fileName + fileType))
               {
                   fileName = "\\" + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
               }
               fileName = fileName + fileType;
               //写文件
               fileStream = new FileStream(savePath + fileName, FileMode.Create);
               m.WriteTo(fileStream);
               StreamWriter sw=new StreamWriter(savePath+".txt");
               sw.WriteLine(binData.Length.ToString());
               sw.Close();
                return "filePath.Replace("\\", "/") + fileName.Replace("\\", "/");
           }
           catch
           {
               return "上传文件出错|1";
           }
           finally
           {
               m.Close();
               fileStream.Close();
           }

       }

有时候下面的函数会报错,也不知道什么原因.   还是用上传控件自带的转换.

#region 将文件转化成2进制流 ConvertFileStreamToByteBuffer
  /// </summary> 
  /// <param name="filename"></param> 
  /// <returns></returns> 
  public byte[] getBinaryFile(string filename)
  {
      if (File.Exists(filename))
      {
          FileStream s = null;
          try
          {
              //打开现有文件以进行读取。 
              s = File.OpenRead(filename);
              return this.ConvertStreamToByteBuffer(s);
          }
          catch
          {
              return new byte[0];
          }
          finally
          {
              s.Close();
          }
      }
      else
      {
          return new byte[0];
      }
  }
  #endregion

  #region 将流文件转化成2进制字节数组Convert FileStream Into Byte
  /// </summary> 
  /// <param name="theStream"></param> 
  /// <returns></returns> 
  public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
  {
      int b1;
      MemoryStream tempStream = new System.IO.MemoryStream();

      try
      {
          while ((b1 = theStream.ReadByte()) != -1)
          {
              tempStream.WriteByte(((byte)b1));
          }
          return tempStream.ToArray();
      }
      catch
      {
          return new byte[0];
      }
      finally
      {
          tempStream.Close();
      }
  }
  #endregion