zoukankan      html  css  js  c++  java
  • 文件以二进制存入数据库和从数据库读取二进制文件


    
    
    if (FileUpload1.FileContent != null)
     {
                    int len = (int)FileUpload1.FileContent.Length;
    
    
                    string path = Server.MapPath("~/test/xx.docx");//"@d:\xx.docx";
                    FileUpload1.SaveAs( path);
                    FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);
             
             FileByteEx = br.ReadBytes(len);
                /*如果长度大于int类型的最大长度时可以考虑分段进行读取
                while(redLen<total){
     
                buffer
    = new byte[size];
     
                buffer
    =br.ReadBytes(size);
     
                redLen
    += size;

                } */



    fs.Close();
     
             br.Close();

            //注只是写个大概的意思,下边创建数据库连接什么的都没有具体写。


            SqlCommand cmd=new Sqlcommand();

            string sql="insert into test([file]) values (@file)";
    SqlParameter par= new SqlParameter("@file", SqlDbType.VarBinary);
            cmd.SqlParameter=
    par;

         SqlCommand cmd = new SqlCommand();
               
            cmd.Connection = new SqlConnection(strCon);
     
            cmd.CommandType = SqlDataSourceCommandType.Text;
               
            cmd.CommandText = sql;


    // 数据库字段设置为 varbinary(MAX)

             

    }

    从数据库取出

    和普通取数据一样放在dataset中,然后从dataset中取出(转换成byte数组)

    FILE_CONTENT_B=(byte[])ds.Tables[0].Rows[0]["FILE_CONTENT_B"];

    通过下面的语句直接打开

    var filebyte = modSysFiles.FILE_CONTENT_B;//创建的文件路径
              
                string path = Server.MapPath("~/test/xx.docx"); //@"D:	estdata" + "xxx.docx";
                //按照路径实例化文件            
                var file = new FileStream(path, FileMode.Create, FileAccess.Write);
    
    
                //实例化一个用于写的 BinaryWriter            
                var bw = new BinaryWriter(file);
                //var bwr  = new BinaryReader
                //将传进来的二进制字符转换为8位无符号整数数组再写进去            
                bw.Write(filebyte);
                bw.Close();
                //关闭二进制流写入器            
                file.Close();  //关闭文件流            
                //创建Process命令            
                var cmd = new System.Diagnostics.Process();
                //创建要运行的文件或者程序            
                var startfile = new System.Diagnostics.ProcessStartInfo
                {
                    FileName = file.Name,
                    //文件完全路径                             
                    WindowStyle = ProcessWindowStyle.Normal,//Windows窗口样式                             
                    UseShellExecute = true//为true,则用默认的打开方式打开,如果是exe等,设置为false                          
                };
                cmd.StartInfo = startfile;
                cmd.Start(); //打开文件
    
    
    
  • 相关阅读:
    jquery城市选择案例
    jquery点击回到页面顶部方法
    jquery动态创建表格
    myeclipse、eclipse中项目复制后(可能无法访问)注意事项 .
    Myeclipse如何整合tomcat
    Java学习路线图
    ftp
    用javascript实现简体和繁体字间的转换
    FTP远程命令集
    JDBC-ODBC桥乱码问题解决方案
  • 原文地址:https://www.cnblogs.com/hbhzz/p/3240002.html
Copyright © 2011-2022 走看看