响应在上下文中不可用?如何解决?

Response is not available in context? How to solve it?(响应在上下文中不可用?如何解决?)
本文介绍了响应在上下文中不可用?如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我保存了一个 pdf 文件以进行流式传输.我想将流保存到响应流.但它总是抛出错误:响应在上下文中不可用.

I save a pdf file to stream. I want to save the stream to Response stream. But it always throw error:Response is not available in contex.

代码如下:

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的第三个组件).

I want to sent client an attachment. How to acheive it??(The above code i use the third component of Spire.PDF).

提前致谢.

推荐答案

您正在启动新线程来处理请求,但您的原始线程可能会继续执行并在新线程开始对响应执行某些操作之前成功终止请求.你必须在原线程中等待新线程完成(你可以创造性地使用异步页面来不阻塞原线程http://msdn.microsoft.com/en-us/magazine/cc163725.aspx).

You are starting new thread to process request, but your original thread likely continues execution and successfully terminates request before your new thread even gets to doing something with the response. You have to wait for completion of the new thread in the original thread (you may be able to creatively use asynchronous pages to not block original thread http://msdn.microsoft.com/en-us/magazine/cc163725.aspx).

这篇关于响应在上下文中不可用?如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)
Why do I get quot;error: ... must be a reference typequot; in my C# generic method?(为什么我会收到“错误:...必须是引用类型?在我的 C# 泛型方法中?)