最近项目需要做电子签章,需要网页打开PDF签章后保存:正好复习哈二进制和流的转换:
文件转换成二进制字符串写入HTTP输出流
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 try 4 { 5 string path = "Tett.PDF"; //获取文件名 6 path = Server.MapPath(path); //获取物理文件路径 7 if (File.Exists(path) == false) 8 throw new Exception("找不到PDF文件"); 9 FileStream fs = File.Open(path, FileMode.Open); 10 byte[] buffer = new byte[fs.Length]; 11 fs.Read(buffer, 0, buffer.Length); 12 fs.Close(); 13 Response.ContentType = "application/pdf"; 14 Response.AddHeader("content-disposition", "filename=pdf"); 15 Response.AddHeader("content-length", buffer.Length.ToString()); 16 Response.BinaryWrite(buffer); 17 } 18 catch (Exception exp) 19 { 20 throw new Exception("错误", exp); 21 } 22 finally 23 { 24 Response.Flush(); 25 Response.Close(); 26 Response.End(); 27 } 28 }