我写的代码成功的将word文挡和图片写入了数据库,并能读取,
但在读取pdf文档时却显示文件格式被破坏,不知什么原因?
请大家指教.

解决方案 »

  1.   

    “将word文挡和图片写入了数据库,并能读取,但在读取pdf文档时却显示文件格式被破坏”
    如果你是将数据库中的文档以二进制流的形式读取的话,那么就需要在读取文件时正确设置文档的格式(ContentType)!
      

  2.   

    数据库中保存了文档格式,图片和word都可以读取
      

  3.   

    试试
             Response.Clear();
    Response.ClearHeaders();
    Response.Buffer = false;
    Response.ContentType = "application/octet-stream";
             Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(fi.FullName,System.Text.Encoding.UTF8));
    Response.AppendHeader("Content-Length",fi.Length.ToString());
    Response.WriteFile(fi.FullName);
    Response.Flush();
    Response.End();
      

  4.   

    查到的
    显示pdf文件:
    private void showfile(string commandstr)
    {
    string filesize,filename;
    byte[] content;
    SqlConnection conn=new SqlConnection(con);
    SqlCommand cmd=new SqlCommand(commandstr,conn);
    conn.Open();
    SqlDataReader sr;
    sr=cmd.ExecuteReader();
    if(sr.Read())
    {
    content=(byte[])sr["SC_image"];
    filesize=content.Length.ToString();
    filename=Common.Util.Get_filename(sr["source_file_name"].ToString());
    if(sr["image_type"].ToString()=="application/pdf")
    filename="x.pdf";
    Response.ContentType = sr["image_type"].ToString();
    Response.AddHeader("Content-Length",filesize);
    Response.AddHeader("Content-Disposition", "inline;filename="+filename);
    Response.BinaryWrite(content);
    }
    sr.Close();
    conn.Close();
    }
      

  5.   

    filename=Common.Util.Get_filename(sr["source_file_name"].ToString());
    这句什么意思?
      

  6.   

    为什么word用相同的代码可以保存和读取,而pdf读取时却显示"文件被破坏并无法修复"?
    郁闷啊!!!
      

  7.   

    代码稍微改一下就可以了,如下:
    string filesize,filename;
    byte[] content;
    SqlConnection conn = new SqlConnection("Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=billding;Data Source=127.0.0.1");
    string commandstr = "select * from ImageStore where ImageId = 6 ";
    SqlCommand cmd=new SqlCommand(commandstr,conn);
    conn.Open();
    SqlDataReader sr;
    sr=cmd.ExecuteReader();
    if(sr.Read())
    {
    content=(byte[])sr["ImageData"];
    filesize=content.Length.ToString();
    filename="temp.pdf"; //"application/pdf")
    Response.ContentType = "application/pdf";//sr["image_type"].ToString();
    Response.AddHeader("Content-Length",filesize);
    Response.AddHeader("Content-Disposition", "inline;filename="+filename);
    Response.BinaryWrite(content);
    }
    sr.Close();
    conn.Close();