在我需要隐藏被服务给用户一个PDF文件的位置,我的asp.net web应用程序之一。试图流与asp.net一个PDF文件时产生“损坏的文件”
因此,我写检索来自它的一个CMS系统上的位置的二进制内容,然后刷新一个字节数组到网络用户的方法。
不幸的是,在下载流时出现错误:“无法打开文件,因为它被damadged”(或类似的东西,当在Adobe Reader中打开文件时)。
问题1:我究竟做错了什么? 问题2:我可以使用这种方法下载大文件吗?
private void StreamFile(IItem documentItem)
{
//CMS vendor specific API
BinaryContent itemBinaryContent = documentItem.getBinaryContent();
//Plain old .NET
Stream fileStream = itemBinaryContent.getContentStream();
var len = itemBinaryContent.getContentLength();
SendStream(fileStream, len, itemBinaryContent.getContentType());
}
private void SendStream(Stream stream, int contentLen, string contentType)
{
Response.ClearContent();
Response.ContentType = contentType;
Response.AppendHeader("content-Disposition", string.Format("inline;filename=file.pdf"));
Response.AppendHeader("content-length", contentLen.ToString());
var bytes = new byte[contentLen];
stream.Read(bytes, 0, contentLen);
stream.Close();
Response.BinaryWrite(bytes);
Response.Flush();
}
为什么不使用Response.WriteFile方法? – 2009-08-19 15:32:57
该文件在文件系统中并不存在。 – Pablo 2009-08-19 15:34:50
更加清楚:该文件存在于CMS数据库中。它是可寻址的url,但我无法真正从文件系统中检索文件。 – Pablo 2009-08-19 15:35:58