I have written a controller action that checks for the existence of a PDF file and then returns the file (if found) to the browser as a download.
public ActionResult GetMyFile(string path)
{
if (String.IsNullOrWhiteSpace(path)) { throw new ArgumentNullException("filename"); }
string downloadFileName = System.IO.Path.GetFileName(path);
// The worker process identity must have read access in both the file system and share
// otherwise this always returns false.
if (System.IO.File.Exists(path))
{
FilePathResult result =
File(path, System.Net.Mime.MediaTypeNames.Application.Pdf, downloadFileName);
return result;
}
return Content("no file", "text-plain");
}
Everything works fine, if the path parameter refers to a local path on disk (e.g. "D:\MyFolder\MyFile.pdf"
), but when the path is a UNC path, the PDF is returned, but rendered as plain text in the browser.
Here's a snippet of what this looks like...
%PDF-1.3 1 0 obj << /Length 1409 /Filter /FlateDecode > stream X���
I�%&/m�{J�J��t��
$ؐ@�������iG#)�*��eVe]f@�흼��{����{����;�N'���?\开发者_Python百科fdl��J�ɞ!���?~|?"~Q�����"�Iw�Ow����:O��,��'?���dg|g/����P��t�Ӄ����:}��Ç������to���}�7��q��/�>O�>�_����9!�g������Y���
|�{�p�?�������?����ܿO����s�>���'ڜH������;��D��>�� ���?��?J��t�m����t�C���O�(i�����H������~H��7JLǎ��K�%ͧ������T1�������S���=��.F.#�ww�����z�wǟҴ~���x�~���Px�������|3�j����c��)f1��O�1�4�����b�qN̔>������跟j�jB?��݃ ����c��.���ڀ�^ofq�,�Pp���g��=Oo� ��7�}��פ�> ��m)}�~�W!�w��Q�0SR�3���2-���&-��Fhy�S�]���HiyF_��� �4�;Q��l�f��|]>ۍ�hc��C��64���|L�4�9Pξ�{#-�?��|���=1Tl��O�����݂6�����a֡�.fe:�����/��1��#�{���������?��|v��}�4�}Cw��!�
���&z�v��4�����Uj0�&���-��������x�i�ģ����|=�9LnI7�&+�gʃ�;��U� m�� M��.�ޏ�D�QvT��ϯ���f���(��������0��������{_������ui�Rid�6���u��a��x"��m��{�o$���
� ����.���{@xu�8ӮR�����Ύ�r��{�m��$��O ��v����=�������X!~,E,�P����mf�2%9{��m����֍b���8���ñ��: �PE��O<�e~jƄ�ߨ���?�Z�������"�Ǟ:����D��N�ߌ����PL��0��U����F4 g�oPW�Ml��#"�~ラ���_�����뾯���?���mG
o�������=�bwGr/��b���?t3(�����t��=[\��O+���c������res����u��0�,G��f��̲qO��\�S��7��q̘�܀,.����Wn$��w�M%�����2ymd�I<͑U��eV�A-|�DڵDz�à�/]��J�|�ݾ'��$.\W��R�>���l|%�a�gj���0|{�R�c�������!�lwv?S��^S E����z��3�����hr�{��R"C�݅o��Ac�*T��Q��IE�6XP5ˮ�j4k��v� D��-�� endstream
Any thoughts as to why this might be happening?
My environment is an ASP.NET MVC 3 application running in IIS on Windows 7. The client is Internet Explorer 9 RC running on the same machine.
The UNC path is a shared folder on the same machine as the web server, and is the same physical location used in the "local path test" (which succeeds).
For permissions I have ensured that the worker process identity for my application pool has Read permissions in both the file system and through the share.
I figured it out.
Changing the final return
statement from:
return Content("no file", "text-plain");
to
return null;
Seems to have fixed the problem. Although I'm not sure as to why that is. I would think that if the first return
(in the if block) was executed, then the final one would have never come into the picture.
I'm happy to accept someone else's answer if they can explain why that is.
精彩评论