代码改变世界

文件导入数据库相关说明

2009-03-22 13:23  key_sky  阅读(338)  评论(0编辑  收藏  举报

      通过web将大文件传入服务器的数据库中,直接写入数据库会发现无法存入成功(在不考虑网络的因素下)。我们需要将文件先复制到服务器的一个物理地址,然后在把服务器物理地址中的文件通过流写入数据库,当然我们有时候也需要对文件写入流后对流数组进行加密,常规做法为将流数组前面或者后面加入几个字符,然后取出来后再将字符去掉。我们也可以通过MD5算法将流数组打乱,读出来的时候再进行算法解密。最后要将临时文件夹中的文件进行删除。

     将上传文件复制到服务器临时文件夹中以后将服务器中临时文件夹文件转化为二进制数组

Code
/// <summary> 
    
/// 将文件转换为二进制数组 
    
/// </summary> 
    
/// <param name="path"></param> 
    
/// <returns></returns> 
    private byte[] PhotoToArray(string path)
    {
        FileStream stream 
= new FileStream(path, FileMode.Open, FileAccess.Read);
        
byte[] bufferPhoto = new byte[stream.Length];
        stream.Read(bufferPhoto, 
0, Convert.ToInt32(stream.Length));
        stream.Flush();
        stream.Close();

        
return bufferPhoto;
    }

     二进制数组存入数据库

Code
 string InsSQL = "insert into sck (scmc,scjs,sclx,scnr,scsj,scscsj,scscr,wjmc) values (@scmc,@scjs,@sclx,@scnr,@scsj,@scscsj,@scscr,@wjmc) ";
            SqlParameter[] parm 
= new SqlParameter[8];
            parm[
0= new SqlParameter("@scmc", SqlDbType.VarChar);
            parm[
1= new SqlParameter("@scjs", SqlDbType.VarChar);
            parm[
2= new SqlParameter("@sclx", SqlDbType.VarChar);
            parm[
3= new SqlParameter("@scnr", SqlDbType.Image);
            parm[
4= new SqlParameter("@scsj", SqlDbType.VarChar);
            parm[
5= new SqlParameter("@scscsj", SqlDbType.VarChar);
            parm[
6= new SqlParameter("@scscr", SqlDbType.VarChar);
            parm[
7= new SqlParameter("@wjmc", SqlDbType.VarChar);
            parm[
0].Value = TB_MatName.Text.Trim();
            parm[
1].Value = TB_JS.Text.Trim();
            parm[
2].Value = ASPxComboBox1.Text.Trim();
            
string Str_path = string.Empty;
            
if (Session["yonghu"== null)
            {
                Str_path 
= Server.MapPath("tempdata/1");
            }
            
else
            {
                Str_path 
= Server.MapPath("tempdata/" + Session["yonghu"].ToString().Trim());
            }
            DirectoryInfo DI 
= new DirectoryInfo(Str_path);
            
if (!DI.Exists)
            {
                DI.Create(); 
            }
            
string path1 = Server.MapPath("tempdata/play.swf");
            
string path2 = Server.MapPath("tempdata");
            
if (Session["yonghu"== null)
            {
                path2 
+= @"/1/play.swf";
            }
            
else
            {
                path2 
+= "/" + Session["yonghu"].ToString().Trim() + "/play.swf";
            }
            FileInfo FI2 
= new FileInfo(path1);
            FileInfo FI3 
= new FileInfo(path2);
            
if (FI2.Exists && !FI3.Exists)
            {
                FI2.CopyTo(path2);
            }
            
if (FileUpload1.HasFile)
            {
                Str_path 
+= "/" + FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"+ 1).Trim();
                FileUpload1.PostedFile.SaveAs(Str_path);
                parm[
3].Value = PhotoToArray(Str_path);
            }
            parm[
4].Value = TB_Time.Text.Trim();
            parm[
5].Value = ASPxDateEdit1.Date.ToShortDateString().Trim();
            parm[
6].Value = TB_Uploader.Text.Trim();
            parm[
7].Value = str;
            SqlConnection conn 
= new SqlConnection();
            conn.ConnectionString 
= ConfigurationManager.ConnectionStrings["test"].ConnectionString;
            SqlCommand sqlcomm 
= new SqlCommand(InsSQL, conn);
            sqlcomm.Parameters.Add(parm[
0]);
            sqlcomm.Parameters.Add(parm[
1]);
            sqlcomm.Parameters.Add(parm[
2]);
            sqlcomm.Parameters.Add(parm[
3]);
            sqlcomm.Parameters.Add(parm[
4]);
            sqlcomm.Parameters.Add(parm[
5]);
            sqlcomm.Parameters.Add(parm[
6]);
            sqlcomm.Parameters.Add(parm[
7]);
            conn.Open();
            sqlcomm.ExecuteNonQuery();
            conn.Close();

     删除服务器中临时文件夹中的文件 

Code
//删除临时文件机制
    private void DelTempInfo()
    {
        DirectoryInfo DI 
= new DirectoryInfo(Server.MapPath("tempdata"));
        DirectoryInfo[] DIArr 
= DI.GetDirectories();
        
for (int i = 0; i < DIArr.Length; i++)
        {
            FileInfo[] FI 
= DIArr[i].GetFiles();
            
for (int j = 0; j < FI.Length; j++)
            {
                
if (FI[j].FullName.Trim().Substring(FI[j].FullName.Trim().LastIndexOf("\\"+ 1!= "play.swf")
                {
                    FI[j].Delete();
                }
            }
        }
    }

 在大文件夹上传的同时,有时候会出现上传不上去的问题。我们需要在web.config配置中设置httpruntime节点

 

Code
<httpRuntime executionTimeout="300" maxRequestLength="409600" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" appRequestQueueLimit="100"/>