我保存一个pdf文件到流。我想保存流到响应流。但是它总是抛出错误:Response is not available in context .
代码如下:
using System;
using System.Threading;
using System.IO;
using Spire.Pdf;
namespace SingleThreadTest
{
public partial class Test : System.Web.UI.Page
{
//[STAThread]
protected void Page_Load(object sender, EventArgs e)
{
}
[STAThread]
protected void Button1_Click(object sender, EventArgs e)
{
////new a thread
ThreadStart threadStart = HTMLToPDF;
Thread thread = new Thread(threadStart);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
void HTMLToPDF()
{
PdfDocument doc = new PdfDocument();
String url = "http://www.e-iceblue.com/";
doc.LoadFromHTML(url, false, true, true);
Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "application/pdf";
using (MemoryStream pdfStream = new MemoryStream())
{
doc.SaveToStream(pdfStream);
using (MemoryStream ms = new MemoryStream())
{
//ms.WriteTo(Response.OutputStream);
Response.OutputStream.Write(pdfStream.ToArray(), 0, pdfStream.ToArray().Length);
}
}
doc.SaveToHttpResponse("Test.pdf", Response, HttpReadType.Save);
doc.Close();
}
}
}
我想给客户端发送附件。如何实现?(以上代码使用了Spire.PDF的第三个组件)。
您正在启动新线程来处理请求,但是您的原始线程可能会继续执行并在新线程处理响应之前成功终止请求。你必须在原始线程中等待新线程的完成(你可以创造性地使用异步页面来不阻塞原始线程http://msdn.microsoft.com/en-us/magazine/cc163725.aspx)。
我有一个类似的任务。我有一个数据集,我需要返回到客户端excel文件。
我用AXD处理程序解决。添加到web配置中对AXD的引用
看例子
<httpHandlers>
<add verb="GET,POST" path="Export.axd" type="YourNameSpace.ExportHandler, YouDLL"/>
</httpHandlers>
见下面的代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Spire;
using Spire.Xls;
namespace SD.Reval.Admin.Services
{
public class ExportHandler : IHttpHandler
{
virtual public bool IsReusable
{
get { return true; }
}
virtual public void ProcessRequest(HttpContext context)
{
try
{
Workbook workbook = new Workbook();
Worksheet worksheet;
int counter = 0;
string methodName = HttpContext.Current.Request.QueryString["methodName"];
string fileName = HttpContext.Current.Request.QueryString["fileName"];
string parameters = HttpContext.Current.Request.QueryString["params"];
if (parameters == null)
parameters = HttpContext.Current.Request.Form["params"];
int workSheetCount = workbook.Worksheets.Count;
string tableName = string.Empty ;
if (methodName.Length > 0 && fileName.Length > 0)
{
DataSet dataSet = (DataSet)ServiceInterface.InternalGenericInvoke(methodName, parameters);
foreach (DataTable dt in dataSet.Tables)
{
if (dt.Columns.Count > 0)
{
tableName = dt.TableName ;
if (counter >= workSheetCount)
worksheet=workbook.Worksheets.Add(tableName);
else
{
worksheet = workbook.Worksheets[counter];
worksheet.Name = tableName;
}
worksheet.InsertDataTable(dt, true, 4, 1, -1, -1);
counter++;
worksheet.AllocatedRange.AutoFitColumns();
worksheet.AllocatedRange.AutoFitRows();
worksheet.Pictures.Add(1, 1, SD.Reval.Admin.Services.ResourceFile.logo_reval );
//Sets header style
CellStyle styleHeader = worksheet.Rows[0].Style;
styleHeader.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
styleHeader.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
styleHeader.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
styleHeader.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
styleHeader.VerticalAlignment = VerticalAlignType.Center;
styleHeader.KnownColor = ExcelColors.Green;
styleHeader.Font.KnownColor = ExcelColors.White;
styleHeader.Font.IsBold = true;
}
}
fileName = fileName + ".xls";
workbook.SaveToHttpResponse(fileName, context.Response);
context.Response.Buffer = true;
context.Response.ContentType = "application/x-msdownload";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName );
}
}
catch (Exception ex)
{
Log.WriteLog(ex);
throw new ApplicationException("The epxport process failed", ex);
}
finally
{
}
}
}
}