本文介绍了从Web应用程序生成的XPS文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我HWAS试图生成从Web应用程序的多页XPS文档,并试图流的奥纳按钮的点击。

I hwas trying to generate a multi page XPS document from a web application and trying to stream that ona button click.

公共类的Class1
{

public class Class1{

protected void btnGenerateLetter_OnClick(object sender, EventArgs e)
{
    try
    {
        string sid = Request.Form["id"];
        byte[] bytes = FlowDocumentToXPS(GenerateLetter(), 640, 800);
        Response.Clear();
        Response.ContentType = "application/vnd.ms-xpsdocument";
        Response.AddHeader("Content-Disposition", "attachment; filename=document.xps");
        Response.OutputStream.Write(bytes, 0, bytes.Length);
        Response.Flush();
        Response.Close();
    }
    catch (Exception ex)
    {
    }

}

private FlowDocument GenerateLetter()
{
    FlowDocument flowDocument = new FlowDocument();

    string Header = "Test Header Message";
    string Body = "Content goes here";
    string Footer = "Footer Text";

    for (int i = 0; i < 3; i++)
    {
        Paragraph header = new Paragraph();
        header.Margin = new System.Windows.Thickness(250, 100, 250, 10);
        header.BreakPageBefore = true;

        header.Inlines.Add(new Run(Header));
        header.Inlines.Add(new LineBreak());
        header.Inlines.Add(new LineBreak());
        header.Inlines.Add(new LineBreak());

        Paragraph body = new Paragraph();
        body.Inlines.Add(new Run(Body));
        body.Inlines.Add(new LineBreak());
        body.Inlines.Add(new LineBreak());

        Paragraph footer = new Paragraph();
        footer.Inlines.Add(new Run(Footer));

        flowDocument.Blocks.Add(header);
        flowDocument.Blocks.Add(body);
        flowDocument.Blocks.Add(footer);
    }
    return flowDocument;
}

public static byte[] FlowDocumentToXPS(FlowDocument flowDocument, int width, int height)
{
    MemoryStream stream = new MemoryStream();
    // create a package
    using (Package package = Package.Open(stream, FileMode.CreateNew))
    {
        // create an empty XPS document   
        using (XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.NotCompressed))
        {
            // create a serialization manager
            XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
            // retrieve document paginator
            DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
            // set page size
            paginator.PageSize = new System.Windows.Size(width, height);
            // save as XPS
            rsm.SaveAsXaml(paginator);
            rsm.Commit();
        }
        return stream.ToArray();
    }
}

}

这wroks的发展environment.But不同的机器上部署时收到此错误。(IIS6)。细

This wroks fine on the development environment.But getting this error when deployed on a different machine.(IIS6).

启动URI:C:\\ Documents和Settings \\ 050583b.syn \\桌面\\ document.xps
应用程序标识:

Startup URI: C:\Documents and Settings\050583b.syn\Desktop\document.xpsApplication Identity:

System.IO.FileFormatException:文件包含损坏的数据。
   在MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(流archiveStream)
   在MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager)
   在MS.Internal.IO.Zip.ZipIOBlockManager.LoadEndOfCentralDirectoryBlock()
   在MS.Internal.IO.Zip.ZipArchive..ctor(流archiveStream,的FileMode模式,FileAccess的访问,布尔流,布尔ownStream)
   在MS.Internal.IO.Zip.ZipArchive.OpenOnStream(流流的FileMode模式,FileAccess的访问,布尔流)
   在System.IO.Packaging.ZipPackage..ctor(流S,的FileMode模式,FileAccess的访问,布尔流)
   在System.IO.Packaging.Package.Open(流流的FileMode packageMode,FileAccess的packageAccess,布尔流)
   在System.IO.Packaging.Package.Open(流流)
   在MS.Internal.Documents.Application.TransactionalPackage..ctor(原流)
   在MS.Internal.Documents.Application.PackageController.MS.Internal.Documents.Application.IDocumentController.Open(Document文件)
   在MS.Internal.Documents.Application.DocumentManager.DispatchOpen(IDocumentController控制器,文档文件)
   在MS.Internal.Documents.Application.DocumentManager&LT;> c__DisplayClass6.b__5(IDocumentController控制器,文件主题)
   在MS.Internal.Documents.Application.ChainOfResponsiblity 2.Dispatch(动作动作,S主题)
   在MS.Internal.Documents.Application.DocumentManager.<>c__DisplayClass6.<OrderByLeastDependent>b__4(Document会员)
   在MS.Internal.Documents.Application.ChainOfDependencies
1.OrderByLeastDependent(T成员,行动对行动)
   在MS.Internal.Documents.Application.DocumentManager.OrderByLeastDependent(DispatchDelegate动作,文档文件)
   在MS.Internal.Documents.Application.DocumentManager.Open(文档文件)
   在MS.Internal.AppModel.ApplicationProxyInternal.InitContainer()
   在MS.Internal.AppModel.ApplicationProxyInternal.Run(InitData initData)

System.IO.FileFormatException: File contains corrupted data. at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(Stream archiveStream) at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager) at MS.Internal.IO.Zip.ZipIOBlockManager.LoadEndOfCentralDirectoryBlock() at MS.Internal.IO.Zip.ZipArchive..ctor(Stream archiveStream, FileMode mode, FileAccess access, Boolean streaming, Boolean ownStream) at MS.Internal.IO.Zip.ZipArchive.OpenOnStream(Stream stream, FileMode mode, FileAccess access, Boolean streaming) at System.IO.Packaging.ZipPackage..ctor(Stream s, FileMode mode, FileAccess access, Boolean streaming) at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming) at System.IO.Packaging.Package.Open(Stream stream) at MS.Internal.Documents.Application.TransactionalPackage..ctor(Stream original) at MS.Internal.Documents.Application.PackageController.MS.Internal.Documents.Application.IDocumentController.Open(Document document) at MS.Internal.Documents.Application.DocumentManager.DispatchOpen(IDocumentController controller, Document document) at MS.Internal.Documents.Application.DocumentManager.<>c__DisplayClass6.b__5(IDocumentController controller, Document subject) at MS.Internal.Documents.Application.ChainOfResponsiblity2.Dispatch(Action action, S subject) at MS.Internal.Documents.Application.DocumentManager.<>c__DisplayClass6.<OrderByLeastDependent>b__4(Document member) at MS.Internal.Documents.Application.ChainOfDependencies1.OrderByLeastDependent(T member, Action action) at MS.Internal.Documents.Application.DocumentManager.OrderByLeastDependent(DispatchDelegate action, Document document) at MS.Internal.Documents.Application.DocumentManager.Open(Document document) at MS.Internal.AppModel.ApplicationProxyInternal.InitContainer() at MS.Internal.AppModel.ApplicationProxyInternal.Run(InitData initData)


推荐答案

我想这个问题是有没有完全得到写进响应字节。
请尝试以下方法,并希望它应该工作。

I guess the problem is the with the bytes not completely getting written into the response.Try the following and hopefully it should work.

HttpContext context = HttpContext.Current;
context.Response.Clear(); 
context.Response.ContentType = "application/vnd.ms-xpsdocument";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=document.xps");
context.Response.End();

这篇关于从Web应用程序生成的XPS文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:59