首页 > 解决方案 > 无法从 SQL 中检索 pdf 文件

问题描述

出于某种原因,无论如何它都无法识别该文件,我无法弄清楚我做错了什么......

Stream fs = fuBOMUpload.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string fileDesc = fuBOMUpload.PostedFile.FileName;

cmd.Parameters.AddWithValue("@contentType", 
fuBOMUpload.PostedFile.ContentType);
cmd.Parameters.AddWithValue("@fileName", fileDesc);
cmd.Parameters.AddWithValue("@Data", bytes);

这就是我保存文件的方式,下载我这样做

 protected void DownloadFile(object sender, EventArgs e)
        {
            string idColaborador = GridFiles.Rows[0].Cells[0].Text; 

            byte[] bytes;
            string fileName;
            string contentType;
            string constr = ConfigurationManager.ConnectionStrings["FolhaRegisto_ConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("spDados"))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action","SELECTALL");
                    cmd.Parameters.AddWithValue("@IdColaborador", idColaborador);
                    cmd.Connection = con;
                    con.Open();

                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        sdr.Read();
                        bytes = (byte[])sdr["Ficheiro"];
                        fileName = sdr["DescricaoDados"].ToString();
                        contentType = sdr["TipoFicheiro"].ToString();

                    }
                    con.Close();
                }
            }

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

即使我添加了 .pdf 扩展名,它也不会像文件损坏或其他东西一样打开文件,但我正在保存所有字节,所以我不知道出了什么问题

标签: c#.net

解决方案


推荐阅读