Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# response.BinaryWrite问题(文本文件包含PDF编码)_C#_Asp.net_Response - Fatal编程技术网

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

C# response.BinaryWrite问题(文本文件包含PDF编码),c#,asp.net,response,C#,Asp.net,Response,所以我的Asp.Net网站上发生了一些非常奇怪的事情 我有两个单独的页面,使用相同的代码下载存储在SQL数据库中的文件 一个页面是该页面的管理员版本,您可以在其中删除和上载文件,还可以查看文件ID 此页面的用户端版本仅显示下载按钮和文件名。该ID被隐藏并用作网格中的数据键,以从数据库中选择文件 这两个页面在开发中都能完美地工作。但是当我切换到我们的生产暂存服务器时,页面的用户端版本在保存和打开时会将一个简单的单行文本文件变成一堆乱七八糟的PDF编码,而页面的管理版本仍然可以正常工作。PDF的下载

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

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

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

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

这两个页面在开发中都能完美地工作。但是当我切换到我们的生产暂存服务器时,页面的用户端版本在保存和打开时会将一个简单的单行文本文件变成一堆乱七八糟的PDF编码,而页面的管理版本仍然可以正常工作。PDF的下载和查看普通文档和word文档与文本文件的功能相同

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

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();
        }
    }
以下是用户下载部分的代码:

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 textbox控件。(我已尝试剥离页面上的所有其他项目,但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
现在一切都很好!但愿我能调试得更接近附件ID

碰巧有两个文档是相同的,我想我下载的是正确的文档。

在您的管理部分:

string AttachmentID = selectedRow.Cells[1].Text;
在用户部分:

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

我必须假设用户部分分配没有达到您期望的效果,因为两者的逻辑不同。验证在每种情况下AttachmentID设置为什么,可能是通过日志记录。

起初我以为这是个问题。但是propper附件下载和视图在开发过程中使用这段代码很好,我已经通过代码来确保DataKey是正确的附件ID。它们之间的区别必须是环境的。考虑数据库和表模式(可能对它们进行差异/比较),或者TeleRik版本。第二个想法是,我将重新评估AtthyTypD.DATAKEY(0)正在选择DATAKEY数组中的0个项。碰巧有两个文档是相同的,我想我下载的是正确的文档。现在我需要选择DataKey[I]I作为我所选行的编号。