我将文件(office文档或图片)用image的方式存入数据库,请教怎样把他们取出来时还原成文件的形式呢?请教,谢谢。

解决方案 »

  1.   

    下载(二进制)(这是将数据绑定在DG,模板列上放了隐藏域存二进制数据)
    private void repClass_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    if(e.CommandName=="download")  
    {     
    System.Web.UI.HtmlControls.HtmlInputHidden rd=new System.Web.UI.HtmlControls.HtmlInputHidden ();
    rd=(System.Web.UI.HtmlControls.HtmlInputHidden)e.Item.FindControl("Hidden1");//将二进制数据读出
    int id=Convert.ToInt32(rd.Value);
    DocumentInfo info =SignUp.Component.Bll.Class.Document.GetByID(id);
    int size=Convert.ToInt32(info.strformat_content.Length);
    byte[] f=new byte[size];
    f=info.strformat_content;
    Random ran = new Random();
    string strfile = DateTime.Now.ToString(@"yyyyMMddHHmmss") 
    + ran.Next(100,999)+"."+info.strfileHZ;
    //string filename=info.strFile_Info;
    string newfile=Server.MapPath("../UpLoad/"+strfile);
    FileStream fs=new FileStream(newfile,FileMode.CreateNew);
    fs.Write(f,0,size);
    fs.Close();
    Response.Redirect("../UpLoad/"+strfile);
    }