mercredi 11 novembre 2015

How to redirect to specific error page in MVC FileDownloadResult

I enable my users to download files, some times the files are archived or do not exist at the location, I currently display a 404, I created a specific error page for this specific scenario but I am unable to display it because I have to return something in the FileDownload Result and I am unable to do a response redirect. Also I tried to return content with javascript but that is also not compatible with FileDownloadResult type. How do I route the user to the intended error page which I have will render from its own controller/action?

   public DownloadFileResult Download(string file)
        {
            try
            {
                string loadLististFileName = file;

                // get the displayed filename, extract the file name
                string fileNamePath = loadLististFileName;
                string fileName = Path.GetFileName(fileNamePath);
                string dirName = Path.GetDirectoryName(fileNamePath);

                string dirPath = dirName.Replace("\\", ",");

                string[] dir = dirPath.Split(',');
                int dirlength = dir.Length;
                string year = dir[dirlength - 3];
                string month = dir[dirlength - 2];
                string day = dir[dirlength - 1];

                var fileData = IOHelper.GetFileData(fileName, dirName);

                fileName = IOHelper.GetPrimaryFileName(file);

                return new DownloadFileResult(fileName, fileData);
            }
            catch (FileNotFoundException)
            {

                //return Content("<script language='javascript' type='text/javascript'>alert('Image not found!');</script>");            

                Response.Redirect("Exception/FileIndex");
            }

        }

Aucun commentaire:

Enregistrer un commentaire