Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

In my website i am uploading an audio file, which is saved in project folder. Then while viewing a page, i want to check whether the uploaded file exists in the project folder. How can i check it.Shall I used this function to get the URL and check whether file existed in that url

C#
public string GetAbsoluteUrl(string relativePath)
      {
          try
          {
              //Get host and port by using Authority
              //Get host only by DnsSafeHost
              string host = HttpContext.Current.Request.Url.Authority;
              string protocol = HttpContext.Current.Request.Url.Scheme;
              return string.Format("{0}://{1}/{2}", protocol, host, relativePath.Trim());
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }


Or just use HttpContext.Current.Server.MapPath + Folder name+ file name and check whether the file exists or not.. Please help me out...

I also want to know what;s the difference between the two
Posted
Comments
Sergey Alexandrovich Kryukov 25-Nov-13 2:30am    
The question makes no sense at all. This is the same as asking "what is the difference between sentence and suffix?" or "between car and plate number".

Read MSDN on each part of API, that's it.

—SA
Arjun Menon U.K 25-Nov-13 3:33am    
Sorry for my lame Question Sergey

1 solution

Use Server.MapPath: It converts a relative-to-your-website path like "~/uploads/MP3/" to a full path specification you can use in the File.Exists method:
C#
if (File.Exists(Server.MapPath("~/Uploads/MP3/" + filename)))
    {
    ... do something with the file
    }

You would only need an absolute URL if you are planning on putting a permanent link to the file directly onto the clients webpage (which isn't a good idea, as it means the folder must be publically available).
 
Share this answer
 
Comments
Arjun Menon U.K 25-Nov-13 3:32am    
Thanks a lot Original Griff...
OriginalGriff 25-Nov-13 3:54am    
You're welcome!

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