Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert from physical path to virtual path

Tags:

c#

asp.net

I have this function that gets the fileData as a byte array and a file path. The error I am getting is when it tries to set the fileInfo in the code bewlo. It says 'Physical Path given, Virtual Path expected'

 public override void WriteBinaryStorage(byte[] fileData, string filePath)
    {
        try
        {
            // Create directory if not exists.
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePath)); //when it gets to this line the error is caught
            if (!fileInfo.Directory.Exists)
            {
                fileInfo.Directory.Create();
            }

            // Write the binary content.
            System.IO.File.WriteAllBytes(System.Web.HttpContext.Current.Server.MapPath(filePath), fileData);
        }
        catch (Exception)
        {
            throw;
        }
    }

When debugging it, is providing the filePath as "E:\\WEBS\\webapp\\default\\images\\mains\\myimage.jpg" . And the error message is

'E:/WEBS/webapp/default/images/mains/myimage.jpg' is a physical path, but a virtual path was expected.

Also, what it is triggering this to happen is the following call

properties.ResizeImage(imageName, Configurations.ConfigSettings.MaxImageSize, Server.MapPath(Configurations.EnvironmentConfig.LargeImagePath));
like image 309
user710502 Avatar asked Nov 17 '11 17:11

user710502


People also ask

Is a physical path but a virtual path was expected?

Generally this physical and virtual path problem occurred whenever we refer “Server. MapPath” value multiple times while using folder path in applications. To solve this e:is a physical path but a virtual path was expected we need to use Server.

What is virtual path and physical path?

First of all, let's get the overview of both. Physical path - This is the actual path the file is located by IIS. Virtual path - This is the logical path to access the file which is pointed to from outside of the IIS application folder.

What is virtual path in asp net?

The virtual path of a web folder is (almost) never the same as the physical folder. In your code you will, reference both the physical path and the virtual path, depending on what you are coding. ASP.NET has 3 tools for working with folder paths: the ~ operator, the Server. MapPath method, and the Href method.

What is a virtual path?

A virtual path (VP) is a collection of virtual channel connections (VC) made through an ATM network. Each VP or VC can be either permanently established or set up dynamically for the time needed to transmit information through the network.


2 Answers

If you already have a physical path, it doesn't make sense to call Server.MapPath.

You're calling MapPath twice.

like image 78
SLaks Avatar answered Sep 21 '22 05:09

SLaks


Working:

    string[] filesPath = Directory.GetFiles(Server.MapPath("~/txtPath/"));        
    foreach (string path in filesPath)
    {
        FileInfo fi = new FileInfo(path);      //This Is Working
       string LastAcceTime = fi.LastWriteTime; //Return Correct Value
    }

Not Working:

    string[] filesPath = Directory.GetFiles(Server.MapPath("~/txtPath/"));        
    foreach (string path in filesPath)
    {
        FileInfo fi = new FileInfo(Server.MapPath(path));  //This Is Worng
       string LastAcceTime = fi.LastWriteTime;             //Return 1/1/1601 
    }

Dont use Server.Mappath twice

like image 28
Purav H Mehta Avatar answered Sep 18 '22 05:09

Purav H Mehta