FileStream响应显示 而不是拉丁字符

时间:2014-07-10 09:26:33

标签: javascript asp.net asp.net-mvc filestream asciiencoding

请仔细阅读整个问题,我在C#ASP.NET MVC中有一个应用程序。哪个Ajax调用函数,该函数使用FileStream返回PDF文件,响应 STREAM A 。并且由于它没有正确呈现文件。该文件只是空白页面,打开时没有文本。 ASP.NET MVC中的FilestreamResult在返回PDF文件时返回响应 STREAM A

STREAM A

%PDF-1.3
%����
7 0 obj
<<
/Type /ExtGState
/ca 1
>>
endobj
5 0 obj
<<
/Type /Page
/Parent 1 0 R
/MediaBox [0 0 595.28 841.89]
/Contents 3 0 R
/Resources 4 0 R
>>
endobj
4 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 6 0 R
>>
/ExtGState <<
/Gs1 7 0 R
>>
>>
endobj
8 0 obj
<<
/Producer (��pdfmake)
/Creator (��pdfmake)
/CreationDate (D:20140526100505Z)
>>
endobj
10 0 obj
<<
/Type /FontDescriptor
/FontName /AAAAAA+Roboto-Regular
/FontFile2 9 0 R
/FontBBox [-479 -271 1170 1058]
/Flags 32
/StemV 0
/ItalicAngle 0
/Ascent 1048
/Descent -271
/CapHeight 1048
/XHeight 0
>>
endobj
6 0 obj
<<
/Type /Font
/BaseFont /AAAAAA+Roboto-Regular
/Subtype /TrueType
/FontDescriptor 10 0 R
/FirstChar 33
/LastChar 53
/Widths [586 568 252 523 249 550 568 874 568 252 529 641 659 583 343 346 568 757 345 878 513]
/Encoding /MacRomanEncoding
/ToUnicode 11 0 R
>>
endobj
2 0 obj
<<
/Type /Catalog
/Pages 1 0 R
>>
endobj
1 0 obj
<<
/Type /Pages
/Count 1
/Kids [5 0 R]
>>
endobj
3 0 obj
<<
/Length 218
/Filter /FlateDecode
>>
stream
x���=N�0�~N1����I�bl
�H6A����שhH,n����Fطs�]�����'
����4��O<��
x^�/Ϋ��J}ӟO��s�ũ�
����C(��땞*���щ"��Kٙ���XD��sŶd�f�a�Ŋ&��b8H9Ⴙ��@|Ѵ+,�Ȥ��}dxu%���Ѷ�ME7S����AXUm�j����xWϿ
endstream
endobj
9 0 obj
<<
/Length 3042
/Filter /FlateDecode
/Length1 4920
>>
stream
x��X
pSU�?����i�&M�R�4��H۴T��� (P*�|I��MhmS�`��f][l�������CR�.*���#�u����CѝYg����s�{)Iq�d������9�w�9�� ���ohq��6��<r��6\I��)^�=��ipd�OoH��,����y_a�a�/ ^���r$�rģ��hqt��
� ь���i��Jn�2ĕ��W������m��sg��W8�� �Ij����g
�cc�ka�Jwf�p������E�mQ;w�8��@���d�����ǵ~2�_@,z��Rl���9�p.�,�_��e�=̬8    �*��vS�FQ�B�b��!�1��ڛvlR��fͲ��\Q�5CTᅝT�y���w���;��A�H���s�u��#ۖ����)��#N����,�EC4� f���3z��/�D~�~~�M���v���/%�!�_��en���,xX����"�����SZ�>y���l�}�uӐ�W�7�\T8�t��ၴDR;����>j8M��2��A`��1NQfVffq�Je�"dLI5��SDkFfI�=5�^8���?2��y����woX�ؙ.�8��HO����^^��ݷu�y\\�y}y�=i9{���d��v�`<7"�����#���D'GXK���Hp��d�_k<�=�E�c�
�Lֲq�0w��VȌ�i�����^,Ӵfeʙ�|��*)��s�5��$��H���>�m��8A��o��B�m::�=�
�dV�_ٷ�ئ:�c����7����6�i]���wqu$���5�]2[9�1�j�u==�.�3W�[3��K�u���;�;��Y�bϭ�;�y����\�h�2_�E��/�H�c|� �D��5$F��`RXX����lC��~�鉮O$Q|��y��pۋ�[[\��?�y�:_:�^V�t�o����.��ٝ�7�C��]����b�<����������52;��_d'�
������8`��!�b�d��!�H�˅��K�Q��   :���p�>�DM��i��w�!?^
_��]�w������ǯ,�]�Xr�y6/7{�$����6���!�H=�k�
rME���\%F{�@<�XY����G�i�����@�E�T��(?�����E�|�F�J�ZbE��,XC����5b8����|�۷˹�dY��/6��Q��CF��}ο�=��/��]�m�R��CS|l��0�=�#\;4X!�e.҄DV��0.@8�l���IJ��$�����:͂��3d!]A�{��p�bxiz���/��.�ߟz����]]8
�/�ї8�e���d>��x�'��&�   h�O�v��z�,������Ar��Nօ_
u���/~(��υ/��>�7v����D#�)��C����]�f�F�V ��jl�g�)]M63�y�����n��&�;]eC�촜�_U��1�����Ɇ'�Z��c�*-�F����CMA�������C�X#��2�d�����"�F��@`a���$�d��~B�B����Jˠ=��MiA�!��5����"��
��@�oT����L7
���F�ZĜ�1�$)S�z&�ꨕ���
���C�ۍV\��g�[����̙yE��)�5ٝ�G�
��7zC��s���ɦ'��'{����P��z
+���*��ܺ��i��6@�}�̯�%�.���m1�ō�eo����V~���(�����
�G��<��33����QGRvW]��7;�V��K/���*�_zS�k.��%vW#l���"AJ�lG�$#H�6H�e   h5uR�A��b-K���UN�O�C��
��Ο��%I�%mJz0����I"K���`7Hgb��T*�J���8�܁/�Ŵ�O�O��5W���?|�yl�v�U��R��Ӵ�8�s��-���Is1_֎�W�ln��a�I��R)���=��o��b�H;>yGeR�3ad�t�ȑ?=��p��d��U�.p�{w��V����X��wt�\��
�=l��.������Z���t
V��`Y���Z;�N��A����y�[�=�~-_;�\��`=���s�K��#�T��,b%*[�Q�H�G�)Bx���L��7|��*ZF��
�MB���Aȅ�_L����j@oqw�N7��$� �%�U]9&��Y��5��E�_CD\�E+��<�    ��ٶ`�a2��p79�
�!� N�$/�SB��J��3$���S:�>K�s�sN�0O�2~���F8,&����z��~7�P�y4J�
����0���́���B��� }姤g��Dȥ���*�M������>�������S�Nz׊��J�T؋G�x"k't��n����3����h(�%�V���7V�JX���o3p���:��A+�'����~���V�8���H��V���EN��ȣ�)z��"�`
�P��E��=�(r�"��QE.@IV�"�}S���T�QAiW�q �Wyh���<4$���!��U�Z0���2ɿy�ѩ�<��{�c?������+�dH������3;>������u76�̋<��f��0��7�L;��8����j@Yt���M��"�y5�Q!NM���{|X�#�x����ո���E�oނ�;�+�+��6+���y{���hEh$F��F��j��3�\�v���\�W��_PT0�Q�cֹP_;�c�ͨ3�|I{s�    �˥O�l2W1�8�n7;�>���jqx7�=���Mpɍ��R\}hցqvA�Di�<��Jlt�d5�2Eq��%�Ni�Z*���v���jF�\^_��*p�����Q7c<e�wʑ�Rw�����4oiu��f_��\��h�e$� aa^>֫
�PW;���q^3:^�\����:�OF����db��F~���Vj�utt�9$�<�F[��E�m��+W.�e^�t���R�O��7-޴����")�z;J�bW���*��,�}�d}BF���c����.����d�TN-:ng��|T0��,n��C�n���jD�}
endstream
endobj
11 0 obj
<<
/Length 317
/Filter /FlateDecode
>>
stream
x�]��n� ��<�,�E��؉dY�ҍ���nÐ"�a��ۇ�D�t�>�|88;o������#�t�u��0��:Q�������,� �������Nd�����W�L�Bs��`�v_��gƫ�?8������&m�G�w9#dl�:�۸���W��y��(��{$�h\�T���������:��R�pLF}� �����P�    K��HX1�k¶"<�9aÂ��eD�#�&<����g��x T�ah3 cChx�2a�����m9o�������5e�8o3q�S#tsϦ�5�T2_/�K�Z��?�/�\����u
endstream
endobj
xref
0 12
0000000000 65535 f 
0000000936 00000 n 
0000000887 00000 n 
0000000993 00000 n 
0000000169 00000 n 
0000000059 00000 n 
0000000612 00000 n 
0000000015 00000 n 
0000000286 00000 n 
0000001283 00000 n 
0000000398 00000 n 
0000004412 00000 n 
trailer
<<
/Size 11
/Root 2 0 R
/Info 8 0 R
>>
startxref
4802
%%EOF

它为ÿö等拉丁字母提供等等。

我希望该文件像

一样流式传输

STREAM B

%PDF-1.3
%ÿÿÿÿ
7 0 obj
<<
/Type /ExtGState
/ca 1
>>
endobj
5 0 obj
<<
/Type /Page
/Parent 1 0 R
/MediaBox [0 0 595.28 841.89]
/Contents 3 0 R
/Resources 4 0 R
>>
endobj
4 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 6 0 R
>>
/ExtGState <<
/Gs1 7 0 R
>>
>>
endobj
8 0 obj
<<
/Producer (þÿpdfmake)
/Creator (þÿpdfmake)
/CreationDate (D:20140527071607Z)
>>
endobj
10 0 obj
<<
/Type /FontDescriptor
/FontName /AAAAAA+Roboto-Regular
/FontFile2 9 0 R
/FontBBox [-479 -271 1170 1058]
/Flags 32
/StemV 0
/ItalicAngle 0
/Ascent 1048
/Descent -271
/CapHeight 1048
/XHeight 0
>>
endobj
6 0 obj
<<
/Type /Font
/BaseFont /AAAAAA+Roboto-Regular
/Subtype /TrueType
/FontDescriptor 10 0 R
/FirstChar 33
/LastChar 53
/Widths [586 568 252 523 249 550 568 874 568 252 529 641 659 583 343 346 568 757 345 878 513]
/Encoding /MacRomanEncoding
/ToUnicode 11 0 R
>>
endobj
2 0 obj
<<
/Type /Catalog
/Pages 1 0 R
>>
endobj
1 0 obj
<<
/Type /Pages
/Count 1
/Kids [5 0 R]
>>
endobj
3 0 obj
<<
/Length 218
/Filter /FlateDecode
>>
stream
x­Ò=NÄ0à~N1Àñ¼ÿIbl
òH6A¬ÄùשhH,nÆúôüFØ·s·]ÙÄåÂÓ'
óÏÇ4¿O<­´
x^§/ΫðûJ}ÓOÌsÊÅ©×
Ï®ÝC(áë*½üÑ"ÂØKÙûXDêsYÌd³f¼aìÅ&§b8H9á¹á@|Ñ´+,­È¤ãö}dxu%ÇÃõѶ­ME7S½óóAXUmÝjÀøËÞxWÏ¿
endstream
endobj
9 0 obj
<<
/Length 3042
/Filter /FlateDecode
/Length1 4920
>>
stream
xX
pSUö?÷¾¤iÚ&MÓR4ÒýHÛ´TÊ ¥ (P*´|I·¦MhmSÒ`ÛÑf][lª¨CR».*®­#ÒuÔÅÿºCÑYgÝÔsß{)IqÖdÞËûûÞ9çwî9çÞ ù°îohq´6 ä<r»6\Iû)^ =ÚìipdÌOoHð¡,¯©Å×y_aÜaÄ/ ^ÓÔär$ÄrÄ£ïhqt¶Ñ
® ÑØÜæi÷ÑJnâ2Ä­Wëô÷ðmø¸sgËÞW8³ð ÀIj¦g
Åccøka·Jwfàp·àØÀ×øEÛmQ;w­8@Ôê×d¯±»íǵ~2_@,z«ÞRlá»Ã9Áp.ß,¯_ûe½=̬8   *¿ÁvSFQ­B bªÐ!Ð1ÀéÚvlRfͲ¦Ñ\Q¦5CTáTßy°áwíÞ;³AîHåêás¡uôð#Ûͽ)ô£#N¼âÝ,EC4â fêü3zî¶/×D~Ü~~Mé­Åvââ/%Æ!_ãØen¯,xXÖÊâ"Ó¤¡æSZ>yöàÓl­}íuÓéWë7é\T8ãtþÄá´DR; Åâ>j8MçÏ2´A`ÚÍ1NQfVffqJe"dLI5¥¦SDkFfI=5Õ^8ã¾?2øÖyè¨woX÷Ø.8ûHO½õî^^Ý·uÇy\\´y}yÙ=i9{·îød·èvÔ`<7"ç³²Á#³D'GXK©ÒHp´âd_k<©=«EcØ
¤LÖ²qÌ0w³VȦiÉÌÌÆÔ^,Ó´feÊ|§Î*)¹sÓ5ñ¤$°áHàÆß>÷m¬ë8AÅËoÑêBßm::å=ë
dVº_Ù·ÿئ:cøÀ©7ý¡ÿ6íi]äï¼wqu$¹ò5À]2[9ñ1Ùjêu==¼.3W¦[3ÌKu°éØ;Á;íYbÏ­ª;óy¨êé\±hô2_ñEô /ÖHäc|Ñ ÐD5$F`RXXÐè¨lC§Ó~Áé®O$Q|°¹yýpÛï[[\¿?½yã:_:ê^Vãt®oþú¡.ºëÙ»7©C÷Ò]û¸§bô<²£Ð¾Üô52;Éí_d'õ
õÄþ¨£8`Ã!bêd±Æ!ñHÖË¢ÒKµQÌÏ  :÷÷¦p>ùDMç´i«wï!?^
_¨¨]þwåð»áëǯ,«]«Xræy6/7{Ì$Àîú6ÊÚë!¬H=©kè
rME®÷Ë\%F{Ò@<¨XYÄÛãGâiíÀ³ñØ@ÐEñTé¦(?¶ÅÅE|FJ¢ZbE,XC´ê«ð5b8ñ÷Ð|۷˹·dYéÛ/6ìÖQòÃCFû}ο=üß/]¸möRîCS|l¥£0=þ#\;4X!óe.ÒDV0.@8ÆløØIJ$¶òÆÞ:Í3d!]A{Ðpøbxizª·¯/¼.üßzÿÊÓý]]8
/Ñ8ôeìd>âËx¯'º&Ê    hOùv½Åz£,ãá£ÖArÜNÖ_
uýú/~(øÃÏ/½>7vÍðÐÇD#Õ)ó£Còã÷]áf©FV ­jl÷gÙ)]M63¸yð¤ú¬n§&Þ;]eCýì´_U©1±§êØÉ'¿ZØcâ*-ÔFÞÂëCMAî½¥ûèÐCX#÷ú2ædÌýÔ"ÐF@`aÔêü$²dö~BÙB¥¬ÎJË =®ÊMiA¿!ïÑ5©Ó¸Ý"Öü
¾ô@õoT¼²ÿ»L7
óÀ÷FÍZÄ1$)S½z&éê¨Óù
Ãþ¶CÑÛV\´g[Ëõ¤ïÌyEúº)©5ÙÝG
óÂ7zC¥sÕâ£É¦'÷Ó'{Àü£Põz
+õ¦*ÀÈܺi±Ò6@ª}Î̯´%.m1¤ÅÛeoõÒúV~¨®¯(ø©øé
§G<±¦33³«¦ûQGRvW]ùÓ7;VôK/«*_zSk.ð%vW#lÿµõ"AJÌlGÈ$#H6HÃe h5uRÝA¢b-KªªUNOµCõ
¥¼ÎÒ%I÷%mJz0éñ¤ýI"K½Ä`7Hgbå²T*ÎJìä8õÜ/ÒÅ´OO÷ÿ5WÌþ¸?|úylûvêUõöRÿÓ´ï8ùs¨-îÄIs1_Ö×WÌlnÙáaäIô¾R)¤è=­Îoö§bÞH;>yGeRÉ3adtáÈ?=±pæ¤dûÔUÕ.p{w½òV¼æ¼ØX¿«wt\ÿâ
¬=lã.¹³ñZÏt
VÙð`YåõZ;ÙNúîAç×Åy´[µ=Ú~-_;°\½`=³µsØKÍÒ#Tô,b%*[¦QHG)BxúöðLÁü7|ÿõ*ZF«È
ßMB¯ÞAȽ_LÅÜ×Âj@oqwëN7ÒÎ$ %ÿU]9&±YÕá5ÛÝEÇ_CD\ÐE+ë¸<Ú    úøÙ¶`ða2ýÛp79ô
ù! Ní$/SBÊûJ´Â3$¿ÝäS:>Ks·sNî0Où2~ÿ­°F8,&«Ðýzï~7ôPúy4J²
°ÉÄ0ôÍÁÂBäÀ }姤gëÅDÈ¥ïÁ*MÁàº>ô¾¦æÍSÿNz×þ¤JTØGÊx"k't¿ônÆÞÇð3¶h(%°Váý°7VÁJXÁo3p´Á:¸ßA+¥'¦¹ã~¤ãVø8üH¡VÓENñíÈ£È)zù"ç`
ìPäÁEÎã=(r¦Â"ÐëQE.@IVä"Ç}S¬TîQAiWäq WyhÉë<4$¨Èã!UäZ0¯¹2É¿yäÑ©<ÖÓ{Æc?¾¤È¡+òdHäî¸ËÓê3;>¹ÁÓÖåu76ùÌ<Æf¹0¿ 7ÿL;¾ö8Á¿¼j@YtÜÐMÒè"y5Q!NM®ðÔ{|X#õxøÀ³ÂÕ¸¥ÙáEoÞ·;À+Û+ï6+·¢y{ÚÒhEh$FÊFí±jþ×3«\Þv·§Õ\W_PT0Q¶cÖ¹P_;cÔͨ3|I{sÆ    ÚË¥O¶l2W1©8àn7;Ì>¯Ãéjqx7=¸æMpɦÌR\}hÖqvADiÊ<øçJltód5Ñ2Eq§%öNi¦Z*¼îvÛÑjFÏ\^_»§*pÚØôµ£Q7c<e·wÊûRw«µÝå4oiuº¼f_Ë\ÞæhÀe$Ç    aa^>Ö«
ºPW;¨q^3:^\å:¼OF±Ïä dbôF~Ï×Vj³uttä9$ò<ÞF[³ìE»méÝ+W.Ìe^°tô¡RüOÇÒ7-Þ´½")Úz;JbW©À¶*±ã,Ä}d}BF±êÁc¦ñ£.éÿ®ÁdµTN-:ng¿§|T0ú,n½ºCªn¥ßýjDú}
endstream
endobj
11 0 obj
<<
/Length 317
/Filter /FlateDecode
>>
stream
x]Ínë ÷<Å,ÓEäßØdYªÒ··ºnÃ"Õa²ðÛD©t¶>à|88;o³²°¨#ëtÀu¹0áÅ:Q ­¿Õ,½ ó¸­çÁºNdÿÒòûW½LøBsÆ`Ýv_çgÆ«÷?8£¾&m÷Gúw9#dlÝ:­Û¸íëWñ¹y({$µh\½T¤» èò¼ïé:ýßRùpLF}Ë º²èúP÷  KÂæHX1k¶"<9aðeDÂ#£&<±µgáÄx TüahÂ3 cChx2açm9oÛÞó­â¼5e¨8o3qS#tsϦÕ5T2_/·K½ZÏ?À/\üÜÿu
endstream
endobj
xref
0 12
0000000000 65535 f 
0000000936 00000 n 
0000000887 00000 n 
0000000993 00000 n 
0000000169 00000 n 
0000000059 00000 n 
0000000612 00000 n 
0000000015 00000 n 
0000000286 00000 n 
0000001283 00000 n 
0000000398 00000 n 
0000004412 00000 n 
trailer
<<
/Size 11
/Root 2 0 R
/Info 8 0 R
>>
startxref
4802
%%EOF

C#CODE

    public FileStreamResult GetFileStream()
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
        string fileName = "template.pdf";
        return new FileStreamResult(new System.IO.FileStream(path + fileName, System.IO.FileMode.Open), "application/pdf");
    }

JAVASCRIPT CODE

function ajaxFileStream() {
    //jQuery.support.cors = true;
    var url = "/Home/GetFileStream";
    $.ajax({
        type: "GET", //POST
        url: url,
        crossDomain: true,
        //data: {},
        success: function (response) {
            console.log(response); // here i get STREAM A 
            //do some operation on response
        },
        error: function (error) {
            console.log("error");
        }
    });
}

是否有任何变化 服务器端:,同时以utf-8或其他类型的某些特性流式传输文件。
客户端: javascript中的一些功能代码,可以从 STREAM A 转换为 STREAM B

欢迎任何建议,提前致谢。)

1 个答案:

答案 0 :(得分:1)

好吧,只需要为ajax请求将responseType设置为arraybuffer。既然,jQuery不支持那个dataType,就得使用传统的Javascript Ajax

有关详细信息,请参阅Sending and Receiving Binary Data

function ajaxFileStream() {
    var url = "/Home/GetFileStream";
    var oReq = new XMLHttpRequest();
    oReq.open("GET", url, true);
    oReq.responseType = "arraybuffer";

    oReq.onload = function (oEvent) {
        console.log(oReq.response);
        var blob = new Blob([oReq.response], { type: "application/pdf" });
        var win = window.open('', '_blank');
        var URL = window.URL || window.webkitURL;
        var dataUrl = URL.createObjectURL(blob);
        win.location = dataUrl;
    };
    oReq.send();
}

谢谢,bUKaneer链接帮了很多。