Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So, below you can see the data we pull from the field in the database, and
what I need it to look like. Can anyone help me out with the code to do
this?

From database:

q:\quotewerks\images\contact systems\contac3s.jpg
q:\quotewerks\images\fuji\cp6-5 cal\mvc-012f(2).jpg
q:\quotewerks\images\front emerald.jpg

Needs to be changed to:

~/images/contact systems/contac3s.jpg
~/images/fuji/cp6-5 cal/mvc-012f(2).jpg
~/images/front emerald.jpg


Any help would be great, thanks!

Best Regards,
Posted

I got the solution by storing the virual path in the database and saving the image using physical path.
.
.
So when I need to display the Image in the Gridview, I display it using the virtual path in the database.
.
.
Physical path :
string strPath = Server.MapPath("~/images/");
                fileupload1.saveAs(strPath);


And I store in the database just :
string strVirPath = "~/images/"+fileupload1.FileName;

.
.
Thanks All for your Answers.

Happy Coding!!!
 
Share this answer
 
v2
Followin Code Snippet might help you:

C#
string sPhysicalLoc="q:\quotewerks\images\contact systems\contac3s.jpg";
string virtualFilePath = sPhysicalLoc.Replace(physicalRootDirectoryPath,"~/");
 
Share this answer
 
v2
code for you

C#
public static class Globals
{
    private static string applicationPath;

    static Globals ()
    {
        Globals.applicationPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
    }

    public static string resolveVirtual (string physicalPath)
    {
        string url=physicalPath.Substring(Globals.applicationPath.Length).Replace('\\', '/').Insert(0, "~/");
        return (url);
    }
}



Read about this in deatil : ASP.NET - Converting a Physical Path to a app relative Virtual Path
[^]
 
Share this answer
 
v2
I think for the purpose specified in the question the Replace method of string class can be used as below
C#
string pPath = @"q:\quotewerks\images\fuji\cp6-5 cal\mvc-012f(2).jpg";
string vPath = pPath.Replace(@"q:\quotewerks","~").Replace(@"\","/");
//vPath ->  ~/images/fuji/cp6-5 cal/mvc-012f(2).jpg
 
Share this answer
 
Comments
Member 12423288 29-Jul-16 7:31am    
what happen if i change location of my application .

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