Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i get file name as shown in code below
but when i apply condition it not working

if (fileName == "NoImage")
   {
         fulpath = "~/img/Employee/NoImage.jpg";
   }


I want if file name is "NoImage" give self path

What I have tried:

HttpPostedFileBase file = Request.Files.Get(0);

string fileName = Path.GetFileName(file.FileName);
     var ext = Path.GetExtension(file.FileName);


if (fileName != null || fileName != String.Empty)
 {
   if (fileName == "NoImage")
   {
         fulpath = "~/img/Employee/NoImage.jpg";
   }
   else
    {
        fulpath = "~/img/Employee/" + MaxCount + "-" + year + ext;
        file.SaveAs(Request.MapPath(fulpath));
    }
   }
Posted
Updated 10-Mar-23 22:19pm
Comments
PIEBALDconsult 10-Mar-23 15:48pm    
A file may have many names, which one do you want?
Member 15627495 10-Mar-23 16:18pm    
look in -----> Path. extensions , you'll find the good one.

We can't be sure, but the most likely thing is that the filename you are comparing against either contains path info, a file type extension, or both - so it fails your test.
But we can't tell, because we can't run your code with your data.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Take a look here: Uploading Files In ASP.NET MVC Using HttpPostedFileBase[^]

I think you neeed something like that:
if (fileName != null)
    fulpath = "~/img/Employee/" + MaxCount + "-" + year + ext;
else
    fulpath = "~/img/Employee/NoImage.jpg";

file.SaveAs(Request.MapPath(fulpath));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900