无法从SQL

时间:2018-10-18 15:00:38

标签: c# .net

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

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扩展名,它也不会像损坏或打开文件那样打开文件,但是我保存了所有字节,所以我不知道出了什么问题

0 个答案:

没有答案