通过URL下载PDF,在c#中提供0字节的损坏文件

时间:2015-06-02 07:54:35

标签: c# pdf clint

byte[] arrayofbyte = new System.Net.WebClient().DownloadData("http://pdfurl");
MemoryStream ms = new MemoryStream(arrayofbyte);

System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.AddHeader("ContentType", "application/pdf; charset=utf-8");
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.Ticks.ToString() + ".pdf" + ";");
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.Close();

1 个答案:

答案 0 :(得分:0)

我认为您可能错过了内容长度标题。

System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", ms.Length.ToString());