response.BinaryWrite问题(文本文件包含PDF编码)

时间:2013-06-04 17:07:28

标签: c# asp.net response

所以我在Asp.Net网站上发生了一些非常奇怪的事情。

我有2个单独的页面,使用相同的代码来下载存储在SQL数据库中的文件。

一个页面是该页面的管理版本,您可以在其中删除和上传文件,还可以查看文件ID。

此页面的用户结束版本仅显示下载按钮和文件名。隐藏ID并将其用作网格中的DataKey,以从数据库中选择文件。

这两个页​​面在开发中都非常有效。但是当我切换到我们的生产登台服务器时,页面的用户端版本将在保存和打开时将一个简单的单行文本文件转换为一堆gargly-goop PDF编码,而页面的管理版本仍能正常运行。 PDF下载和查看普通文档和单词文档与文本文件的作用相同。

以下是管理员下载部分的代码:

protected void btnSaveAttachment_Click(object sender, EventArgs e)
    {
        GridViewRow selectedRow = ((LinkButton)sender).NamingContainer as GridViewRow;

        string AttachmentID = "";
        string AttachmentName = "";
        if (selectedRow.Cells[1].Text != null && selectedRow.Cells[2].Text != null)
        {
            AttachmentID = selectedRow.Cells[1].Text;
            AttachmentName = selectedRow.Cells[2].Text;
        }

        byte[] objData = Utility.SaveAttachmentBytes(AttachmentID);

        HttpResponse response = HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ClearHeaders();
        response.AddHeader("Cache-Control", " no-store, no-cache ");
        response.AddHeader("Content-Disposition", "attachment; filename=" + AttachmentName + ";");
        response.BinaryWrite(objData);
        response.Flush();

        if (response != null)
        {
            response.End();
        }

    }

以下是用户下载部分的代码:

protected void btnSaveAttachment_Click(object sender, EventArgs e)
    {
        GridViewRow selectedRow = ((LinkButton)sender).NamingContainer as GridViewRow;

        //string AttachmentID = selectedRow.Cells[1].Text;
        string AttachmentID = grdAttachments.DataKeys[0].Value.ToString();
        string AttachmentName = "";
        if (selectedRow.Cells[1].Text != null)
        {
            AttachmentName = selectedRow.Cells[1].Text;
        }

        byte[] objData = Utility.SaveAttachmentBytes(AttachmentID);

        HttpResponse response = HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ClearHeaders();
        response.AddHeader("Cache-Control", " no-store, no-cache ");
        response.AddHeader("Content-Disposition", "attachment; filename=" + AttachmentName + ";");
        response.BinaryWrite(objData);
        response.Flush();

        //File.Delete(SavePath);

        if (response != null)
        {
            response.End();
        }
    }

我正处于一个我不知道到底发生了什么的地方。页面前端代码中唯一的另一个区别是用户页面将数据库中的某些文本加载到标签中并具有telerik文本框控件。 (我已尝试剥离页面上的所有其他项目,但PDF以外的文件仍尝试编码为PDF。)

以下是保存和打开单行文本文件时的样子。

%PDF-1.4
%âãÏÓ
9 0 obj<</H[516 160]/Linearized 1/E 5419/L 14363/N 2/O 12/T 14137>>
endobj

xref
9 11
0000000016 00000 n
0000000676 00000 n
0000000516 00000 n
0000000753 00000 n
0000000881 00000 n
0000000976 00000 n
0000001511 00000 n
0000001903 00000 n
0000002142 00000 n
0000002387 00000 n
0000002463 00000 n
trailer
<</Size 20/Prev 14127/Root 10 0 R/Info 8 0 R/ID[<3d8f2faf909b30f75011a461bd4aff97><b62071c85b58ab4d8f0b23af1811506f>]>>
startxref
0
%%EOF

11 0 obj<</Length 82/Filter/FlateDecode/L 90/S 53>>stream
xÚb```f``
‘BVœÀ   cf`aàXÀàΰ…Ql
HT  ÈAPÌÀàÃÀÃì ,³Ñ†Ë`%·Hˆ…Aý!fb€  O{Ì
endstream
endobj
10 0 obj<</Pages 6 0 R/Type/Catalog/PageLabels 4 0 R/Metadata 7 0 R>>
endobj
12 0 obj<</Contents 19 0 R/Type/Page/Parent 6 0 R/Rotate 0/MediaBox[0 0 612 792]/CropBox[0 0 612 792]/Resources 13 0 R>>
endobj
13 0 obj<</Font<</TT2 14 0 R/TT4 15 0 R>>/ProcSet[/PDF/Text]/ExtGState<</GS1 18 0 R>>>>
endobj
14 0 obj<</Type/Font/Encoding/WinAnsiEncoding/BaseFont/TimesNewRomanPSMT/FirstChar 32/LastChar 150/Subtype/TrueType/FontDescriptor 16 0 R/Widths[250 0 0 0 0 833 778 0 333 333 0 564 250 333 250 278 500 500 500 500 500 500 500 500 500 500 278 0 0 0 0 0 0 722 667 667 722 611 556 722 0 333 389 0 611 889 722 722 556 722 667 556 611 722 722 944 722 722 0 0 0 0 0 0 0 444 500 444 500 444 333 500 500 278 278 500 278 778 500 500 500 500 333 389 278 500 500 722 500 500 444 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 500]>>
endobj
15 0 obj<</Type/Font/Encoding/WinAnsiEncoding/BaseFont/TimesNewRomanPS-BoldMT/FirstChar 32/LastChar 121/Subtype/TrueType/FontDescriptor 17 0 R/Widths[250 0 0 0 0 0 0 0 0 0 0 0 0 0 250 0 500 500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 722 722 0 0 0 0 389 0 0 0 0 0 778 0 0 0 556 667 722 0 0 0 0 0 0 0 0 0 0 0 500 0 444 0 444 333 500 0 278 0 0 0 833 556 0 0 0 444 389 333 556 500 722 0 500]>>
endobj
16 0 obj<</Type/FontDescriptor/FontBBox[-568 -307 2028 1007]/FontName/TimesNewRomanPSMT/Flags 34/StemV 82/CapHeight 656/XHeight 0/Ascent 891/Descent -216/ItalicAngle 0/FontFamily(Times New Roman)/FontStretch/Normal/FontWeight 400>>
endobj
17 0 obj<</Type/FontDescriptor/FontBBox[-558 -307 2034 1026]/FontName/TimesNewRomanPS-BoldMT/Flags 34/StemV 136/CapHeight 656/XHeight 0/Ascent 891/Descent -216/ItalicAngle 0/FontFamily(Times New Roman)/FontStretch/Normal/FontWeight 700>>
endobj
18 0 obj<</Type/ExtGState/SA false/OP false/SM 0.02/op false/OPM 1>>
endobj

etc....................

endstream
endobj
4 0 obj<</Nums[0 5 0 R]>>
endobj
5 0 obj<</S/D>>
endobj
6 0 obj<</Count 2/Kids[12 0 R 1 0 R]/Type/Pages>>
endobj
7 0 obj<</Length 3339/Type/Metadata/Subtype/XML>>stream
<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>
<?adobe-xap-filters esc="CRLF"?>
<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='XMP toolkit 2.9.1-13, framework 1.6'>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:iX='http://ns.adobe.com/iX/1.0/'>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:pdf='http://ns.adobe.com/pdf/1.3/' pdf:Producer='Acrobat Distiller 6.0 (Windows)'></rdf:Description>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:xap='http://ns.adobe.com/xap/1.0/' xap:CreatorTool='PScript5.dll Version 5.2' xap:ModifyDate='2005-06-10T14:07:36-04:00' xap:CreateDate='2005-06-10T14:07:36-04:00'></rdf:Description>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:xapMM='http://ns.adobe.com/xap/1.0/mm/' xapMM:DocumentID='uuid:2aee5402-c607-44cd-a815-8ad3a0bf0a56'/>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:dc='http://purl.org/dc/elements/1.1/' dc:format='application/pdf'><dc:title><rdf:Alt><rdf:li xml:lang='x-default'>Microsoft Word - 1 - DTOD Overview.doc</rdf:li></rdf:Alt></dc:title><dc:creator><rdf:Seq><rdf:li>AttardA</rdf:li></rdf:Seq></dc:creator></rdf:Description>
</rdf:RDF>
</x:xmpmeta>                 
<?xpacket end='w'?>
endstream
endobj
8 0 obj<</ModDate(D:20050610140736-04'00')/CreationDate(D:20050610140736-04'00')/Title(Microsoft Word - 1 - DTOD Overview.doc)/Creator(PScript5.dll Version 5.2)/Producer(Acrobat Distiller 6.0 \(Windows\))/Author(AttardA)>>
endobj
xref
0 9
0000000000 65535 f
0000005419 00000 n
0000005544 00000 n
0000005638 00000 n
0000010369 00000 n
0000010402 00000 n
0000010425 00000 n
0000010482 00000 n
0000013897 00000 n
trailer
<</Size 9>>
startxref
116
%%EOF

分辨

问题是DataKeys [0]总是选择键组中的0项。我改为:

string AttachmentID = grdAttachments.DataKeys[selectedRow.RowIndex].Value.ToString();

现在一切正常!希望我能调试更接近attachmentID!

恰好有两个文件是相同的,我虽然正在下载正确的文件。

1 个答案:

答案 0 :(得分:0)

在您的管理部分:

string AttachmentID = selectedRow.Cells[1].Text;

在您的用户部分:

string AttachmentID = grdAttachments.DataKeys[0].Value.ToString();

我必须假设用户部分分配没有按照您的预期进行,因为两者的逻辑并不相同。在每种情况下验证AttachmentID的设置,可能是通过记录。