在mvc中下载文件时出现异常

时间:2018-04-30 18:14:36

标签: asp.net-mvc

每当我下载文件时都会抛出异常。 这是例外。

Server Error in '/' Application.
Could not find file 'F:\Caliber\CaliberCoaching\CaliberCoaching\App_Data\UploadData\New.docx'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.FileNotFoundException: Could not find file 'F:\Caliber\CaliberCoaching\CaliberCoaching\UploadData\New.docx'.

这是我的下载代码

 public FileResult Download()
            {
                DirectoryInfo dirInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/App_Data/UploadData/New.docx"));


                string contentType = string.Empty;
                string CurrentFileName = dirInfo.FullName ;

                if (CurrentFileName.Contains(".pdf"))
                {
                    contentType = "application/pdf";
                }

                else if (CurrentFileName.Contains(".docx"))
                {
                    contentType = "application/docx";
                }
                return File(CurrentFileName, contentType);  
            }

0 个答案:

没有答案