Return file does not exist when using Action Result as returen type in mvc

I have an action link

    @Html.ActionLink(@Resources.Resource_ar.Download, "DownloadFile", new { Communicationid = item.Communicationid }, new { @class = "btn btn-success btn_Download" })

      

call action to download file

   public ActionResult DownloadFile(string Communicationid)
    {


        string pathString = Settings.getSettingValue("FolderForSaveCommunicationsAttachments");
        //string FullPath = Path.Combine(Server.MapPath("~/Content/UploadedAttachment/"), FileName);
        string FullPath = Path.Combine(pathString, Communicationid);


        Communications ObjCommunication = new Communications(int.Parse(Communicationid));
        string FileName = ObjCommunication.s_FileName;

        //return File(FullPath, "text/docx");

        if (System.IO.File.Exists(FullPath))
        {
            string contentType = string.Empty;

            if (FileName.Contains(".pdf"))
            {
                contentType = "application/pdf";
            }
            else if (FileName.Contains(".docx"))
            {
                contentType = "application/docx";
            }
            else if (FileName.Contains(".doc"))
            {
                contentType = "application/doc";
            }
            else if (FileName.Contains(".jpeg"))
            {
                contentType = "image/jpeg";
            }
            else if (FileName.Contains(".jpg"))
            {
                contentType = "image/jpg";
            }
            else if (FileName.Contains(".png"))
            {
                contentType = "image/png";
            }
            else if (FileName.Contains(".bmp"))
            {
                contentType = "image/bmp";
            }
            else if (FileName.Contains(".xlsx"))
            {
                contentType = "application/xlsx";
            }
            else if (FileName.Contains(".Exl"))
            {
                contentType = "application/Exl";
            }
            else if (FileName.Contains(".txt"))
            {
                contentType = "application/txt";
            }
            return File(FullPath, contentType, FileName);
        }
        else
        {
            return;
        }

    }

      

the problem is that when the file exists, it returns the file and downloads it correctly, but when the file does not exist, I want to show a warning to the user telling him that the file does not exist, which is what I should return I am trying to return javascript ("alert (" file does not exist ")") it gave me a blank page with text I set that "alert ('file not exist')" any help with this problem thanks in advance

+3


source to share


1 answer


You can do something like this:

 if (System.IO.File.Exists(FullPath))
                { //....
                }
                else { return Content("Some error message"); }

      



But I would rather return 404 if this file is not there.

+1


source







All Articles