zoukankan      html  css  js  c++  java
  • 转:用Sql Server存储上载图片字体

    在学习Asp.net开发Web应用程序的时候,我遇上这样一个问题.就是我们常见的在网站上传图片,文件之类 

    的,这在asp.net中似乎很简单,不就一个HttpPostedFile就搞定了吗?呵呵.的确,HttpPostedFile非常方便 

    的给我们提供了从Local to Host的HttpPostedFile类.但是我在想,可否用数据库来存储这些图片或文件 

    呢?答案是肯定的,计算机上的任何数据都是以二进制存储的.下面本着我这两天的学习,来总结一下.... 

    思路: 
    任何计算机上的数据都以二进制存储.我们只需将数据以二进制的形式保存到数据库即可. 
    这里我们使用Sql Server数据库.用Image类型来保存二进制. 


    步骤: 
    1.当然是在Sql Server中建一个存储上传数据的表咯. 

    id:上载数据内容的md5值确保数据库中无重复数据,可用作文件名 
    data:上载文件数据 
    type:文件扩展名 
    length:数据长度 
    time:上载时间 

    CREATE TABLE [picture] ( 
     [id] [char] (32) COLLATE Chinese_PRC_CI_AS NOT NULL , 
     [data] [image] NULL , 
     [type] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , 
     [length] [int] NULL , 
     [Time] [datetime] NULL , 
     CONSTRAINT [PK_picture] PRIMARY KEY  CLUSTERED  
     ( 
      [id] 
     )  ON [PRIMARY]  
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 
    GO 

    2.创建一个HtmlInputFile控件对象myFile.上传的数据就是通过这个对象myFile传递进来的.它有一个重 

    要属性PostedFile,它是一个HttpPostedFile对象 
    PostedFile.ContentLength: 文件长度  
    PostedFile.ContentType:  文件类型 
    PostedFile.FileName  文件名 
    PostedFile.InputStream  文件流数据 
    PostedFile.Save()  将文件保入磁盘 


    3.转换上载的数据和处理要写入数据库中的数据 
    客户端上载的数据是以流的形式保存在HttpPostedFile对象的InputStream中. 
    接下来要做的就是处理数据流,将它转换成二进制数据 
    //----------------转换二进制--------------------// 
    InputStream.Seek(0,SeekOrigin.Begin); // 注意使用确保Position在开始 
    byte[] bdata = new byte[Length];  
    Data.Read(bdata,0,Length);  
      
    //----------------以MD5储存ID-------------------// 
    InputStream.Seek(0,SeekOrigin.Begin); 
    BinaryReader br = new BinaryReader(InputStream); 
    char[] cd = new char[Length]; 
    cd = br.ReadChars(Length); 
    string id = new string(cd); 
    id = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(id,"MD5"); 

    //---------------其它存储数据--------------------// 
    string ExtName = PostedFile.FileName.Substring(PostedFile.FileName.LastIndexOf(".")+1) //取 

    扩展名 
    string Length = PostedFile.ContentLength //文件大小 

    4.ADO操作 
    SqlConnection myConnection = new SqlConnection(ConString); 
    SqlCommand UpLoadCommand = new SqlCommand("Insert into picture(id,data,type,length,time)  

    values(@id,@data,@type,@length,getdate())",MyClass.Con); 
    UpLoadCommand.Parameters.Add("@id",id); 
    UpLoadCommand.Parameters.Add("@data",bdata); 
    UpLoadCommand.Parameters.Add("@type",ExtName); 
    UpLoadCommand.Parameters.Add("@length",Length); 

    myConection.Open(); 
    UpLoadCommand.ExecuteNonQuery(); 
    myConnection.Close(); 

    //---------------------------------------------------------------------------------------------------------------- 
    读取数据库图片就不用我说了吧..Response有一个为二进制准备的BinaryWrite(byte[] buffer)方法.用它就可以把图片输出到IE上了 

    下面是我的源码,把功能独立到类中了.小弟没学过C#,代码漏洞百出,呵呵.献丑了. 

      

    using System; 
    using System.IO; 
    using System.Web; 
    using System.Data; 
    using System.Data.SqlClient; 

    namespace Pic 
    ...{ 
        /**//// <summary> 
        /// MyClass 的摘要说明。 
        /// </summary> 
        public class MyClass 
        ...{ 
            public MyClass() 
            ...{ 
                 
            } 
            public static string GetMD5String(string password) 
            ...{ 
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password,"MD5"); 
            } 
            public static SqlConnection Con = new SqlConnection("server=.;uid=sa;pwd=network;initial catalog=MyHomePage;"); 
        } 
        public class MyUpLoad 
        ...{ 
            public MyUpLoad(HttpPostedFile UpLoadFile) 
            ...{ 
                Data = UpLoadFile.InputStream; 
                FileName = UpLoadFile.FileName.ToString(); 
                Length = UpLoadFile.ContentLength; 
                FileType =FileName.Substring(FileName.LastIndexOf(".")+1); 
            } 
            public byte[] GetByte() 
            ...{ 
                Data.Seek(0,SeekOrigin.Begin); 
                byte[] bdata = new byte[Length]; 
                Data.Read(bdata,0,Length); 

                return bdata; 
            } 
            public string GetID() 
            ...{ 
                Data.Seek(0,SeekOrigin.Begin); 
                BinaryReader br = new BinaryReader(Data); 
                char[] cd = new char[Length]; 
                cd = br.ReadChars(Length); 
                string id = new string(cd); 
                return MyClass.GetMD5String(id); 
            } 
            public bool UpLoadData() 
            ...{ 
                SqlCommand UpLoadCommand = new SqlCommand("Insert into picture(id,data,type,length,time) values(@id,@data,@type,@length,getdate())",MyClass.Con); 
                UpLoadCommand.Parameters.Add("@id",GetID()); 
                UpLoadCommand.Parameters.Add("@data",GetByte()); 
                UpLoadCommand.Parameters.Add("@type",FileType); 
                UpLoadCommand.Parameters.Add("@length",Length); 
                 
                MyClass.Con.Open(); 
                try 
                ...{ 
                    UpLoadCommand.ExecuteNonQuery(); 
                    MyClass.Con.Close(); 
                    return true; 
                } 
                catch 
                ...{ 
                    MyClass.Con.Close(); 
                    return false; 
                } 
                 
            } 
            public int Length; 
            public string FileName; 
            public string FileType; 
            Stream Data; 
        } 

       MyUpLoad myLoad = new MyUpLoad(myFile.PostedFile); 
       Response.Write(myLoad.UpLoadData());

    本文摘自:拾金者(http://www.xkde.com) 详细出处参考:http://www.xkde.com/ComContent-95-8787.aspx

  • 相关阅读:
    HDU 4348 To the moon(可持久化线段树)
    HDU 5875 Function 大连网络赛 线段树
    HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
    HDU 5876 大连网络赛 Sparse Graph
    HDU 5701 中位数计数 百度之星初赛
    CodeForces 708B Recover the String
    Java实现 蓝桥杯 算法提高 套正方形(暴力)
    ASP.NET生成验证码
    ASP.NET生成验证码
    ASP.NET生成验证码
  • 原文地址:https://www.cnblogs.com/wantingqiang/p/1216185.html
Copyright © 2011-2022 走看看