But how could you live and have no story to tell!
访问统计 original graphics

1、存入数据库

 

       int DocLen = FileUpload1.PostedFile.ContentLength;
        byte[] Docbuffer = new byte[DocLen];
        Stream objStream = FileUpload1.PostedFile.InputStream;
        objStream.Read(Docbuffer, 0, DocLen);

        SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["BaseConnectionString"].ConnectionString);
        SqlCommand mycmd = new SqlCommand();
        mycmd.Connection = myconn;
        myconn.Open();
        mycmd.CommandText = "INSERT INTO Temp1(FileType,FileSize,FileContent) VALUES ('" + FileUpload1.FileName.ToString().Substring(FileUpload1.FileName.Length - 4) + "','" + DocLen.ToString() + "',@FileContent)";
        SqlParameter paramData = new SqlParameter("@FileContent", SqlDbType.Image);
        paramData.Value = Docbuffer;
        mycmd.Parameters.Add(paramData);
        mycmd.ExecuteNonQuery();
        myconn.Close();

 

2、从数据库中读取

SqlCommand mycmd= new SqlCommand("SELECT FileType,FileSize,FileContent FROM Temp1 WHERE ID = 1", myconn);
        SqlDataReader mydr = mycmd.ExecuteReader();
        if (mydr.Read())
        {
            Response.Clear();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString() + HttpUtility.UrlEncode( + mydr["FileType"].ToString(), System.Text.Encoding.UTF8));

            byte[] buffer = new Byte[10240];
            long datalen = mydr.GetBytes(2, 0, null, 0, 0);

            long curPos = 0;
            long readsize = 0;
            readsize = mydr.GetBytes(2, curPos, buffer, 0, 10240);
            while (readsize == 10240)
            {
                curPos += readsize;
                Response.BinaryWrite(buffer);
                Response.Flush();
                readsize = mydr.GetBytes(2, curPos, buffer, 0, 10240);
            }
            byte[] rBuf = new Byte[readsize];
            mydr.GetBytes(2, curPos, rBuf, 0, (int)readsize);
            Response.BinaryWrite(rBuf);
            Response.Flush();
        }
        mydr.Close();

posted on 2008-08-18 16:48  nextsoft  阅读(922)  评论(0编辑  收藏  举报