Validate File Path

Using C# MVC, I have a global utility to check if a file path exists:

public class Utility
    {
        // Utility Method to validate file paths
        public bool isPath(string filePath)
        {
            if (System.IO.File.Exists(filePath))
            {
                return false;
            }
            return true;
        }
    }

Open in new window


In my controller, I need to validate a path and if true return a file:

string absolutePath = ConfigurationManager.AppSettings["FilePath"] + FILENAME + doc.id + "." + doc.extension;
return (utl.isPath(absolutePath) == true) ?  File(absolutePath, "application/pdf", doc.name + "." + doc.extension)  : null;

Open in new window


I am getting errors that I can't convert FilePathResult to a string.
Crystal RouseTask LeadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
just a note:  you can shorten your method:

        public bool isPath(string filePath)
        {
            return System.IO.File.Exists(filePath);
        }

Open in new window


And this:
(utl.isPath(absolutePath) == true)

Open in new window


Can just be:

(utl.isPath(absolutePath))

Open in new window


End note..

Question...  what are you intending this to return?

 File(absolutePath, "application/pdf", doc.name + "." + doc.extension) 

Open in new window

Crystal RouseTask LeadAuthor Commented:
I am trying to return a file saved in the validated absolutePath
Chris StanyonWebDevCommented:
The File() method returns a FilePathResult, which you look like you're trying to return. The error you're getting indicates that the return type of the calling method is string:

public string DoSomething()
{
    ...
    string absolutePath = ConfigurationManager.AppSettings["FilePath"] + FILENAME + doc.id + "." + doc.extension;
    return (utl.isPath(absolutePath) == true) ?  File(absolutePath, "application/pdf", doc.name + "." + doc.extension)  : null;
}

Open in new window

This will generate the error you get because the DoSomething() method expects to return a string and we're returning a FilePathResult.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Crystal RouseTask LeadAuthor Commented:
public string FILENAME = "MyFiles_";

The complete method
[HttpGet]
public string DownloadDoc(int id)
        //public ActionResult DownloadDoc(int id)
        {
            CodeFirstDB db = new CodeFirstDB();
            Utility utl = new Utility();

            Documents doc = (from x in db.Documents
                                    where x.id == id
                                    select x).Single();

            string absolutePath = ConfigurationManager.AppSettings["FilePath"] + FILENAME + doc.id + "." + doc.extension;      
            return (utl.isPath(absolutePath)) ?  File(absolutePath, "application/pdf", doc.name + "." + doc.extension) : null;

}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.