【发布时间】:2011-01-01 05:36:51
【问题描述】:
如何通过C#下载存储在数据库、sql server中的word文档作为内容数据?
这是我正在使用的代码:
string doctype = dtResumeInfo.Rows[0]["ContentType"].ToString();
string docname = dtResumeInfo.Rows[0]["FileName"].ToString();
//
try
{
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = doctype;
Response.AddHeader("Content-Disposition",
"attachment; filename=" + docname);
//
//Code for streaming the object while writing
const int ChunkSize = 1024;
byte[] buffer = new byte[ChunkSize];
byte[] binary = (dtResumeInfo.Rows[0]["ContentData"]) as byte[];
MemoryStream ms = new MemoryStream(binary);
int SizeToWrite = ChunkSize;
for (int i = 0; i < binary.GetUpperBound(0) - 1; i = i + ChunkSize)
{
if (!Response.IsClientConnected) return;
if (i + ChunkSize >= binary.Length)
SizeToWrite = binary.Length - i;
byte[] chunk = new byte[SizeToWrite];
ms.Read(chunk, 0, SizeToWrite);
Response.BinaryWrite(chunk);
Response.Flush();
}
Response.Close();
}
catch (Exception ex)
{
}
但是,它显示以下错误:
SubStatusCode 'Response.SubStatusCode' threw an exception of type 'System.PlatformNotSupportedException'
base {"This operation requires IIS integrated pipeline mode."} System.NotSupportedException {System.PlatformNotSupportedException}
Headers 'Response.Headers' threw an exception of type 'System.PlatformNotSupportedException'
我发布的 IIS 版本是 6.0。只能在 IIS 7 中下载 word 文档。所以,这就是错误的原因。有没有其他方法可以克服这个问题?任何其他代码将支持较低版本?请帮帮我。 (对不起,长消息..)
【问题讨论】:
标签: c# asp.net sql-server file-upload