c# - 无法从 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 扩展名,它也不会像文件损坏或其他东西一样打开文件,但我正在保存所有字节,所以我不知道出了什么问题
解决方案
推荐阅读
- html - Node.js/Express.js - 如何将远程 HTML 文件呈现给客户端?
- vhdl - 如何从用户 VHDL 获取多个输入
- c - 使用宏转换结构中整数字段的字节序
- operating-system - 我们是否可以在无限或荒谬的数量下获得 0 页面错误率?
- python - pandas 调整 Excel 表的大小以匹配较大的表以进行比较
- css - 向上滚动时奇怪的消失网站(仅在 Chrome 中)
- reactjs - 在初始页面加载后,是否可以将当前组件作为 React.Suspense 的“后备”内容安装?
- powershell - '[' 后缺少类型名称
- tensorflow - 构建 TensorFlow 时链接 MKL 失败
- python - Python np.asarray 不返回真实形状