【发布时间】:2011-08-18 01:20:36
【问题描述】:
我有一个带有“下载”链接的网页。
我使用 jQuery 执行 Ajax Get 到 ASHX 文件。
在 ASHX 中,我得到了文件的流。然后我将流转换为字节数组并将字节数组返回给调用的 html 页面;
jQuery
$(".DownloadConvertedPDF").click(function () {
var bookId = $(this).attr("bookId");
$.get('/UserControls/download.ashx?format=pdf&bookId=' + bookId, {}, function (data) { });
});
C#
context.Response.ContentType = "Application/pdf";
Stream fileStream = publishBookManager.GetFile(documentId);
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
我没有收到错误消息,但 PDF 也没有显示在屏幕上。
理想情况下,我希望返回的 pdf 和 jQuery 在浏览器中的单独选项卡中启动 pdf。
我怎样才能做到这一点,或者我做错了什么?
【问题讨论】:
标签: c#