'Server.MapPath changes file path when passed outside of Class

I abbreviated my code here and hope convey enough data to express the problem I am having. I am more than happy to elaborate as needed.

Background:

I have an asp site that has about 70 pages that open files from various locations. In one scenario I do some file manipulation, like copy, rename, convert to PDF etc. This is done my moving the file into the project and then eventually serving the file from a project folder.

Originally I created a class with a few functions. I call the function from the web page and the class manipulates and then opens the file.

 Dim ReturnValue As String = OpenMyFile.OpenQCBD(Doc_Id)

The function would manipulate the file and the open it (note the creation of the file path)

OpenTempFile(HttpContext.Current.Server.MapPath(fpath & "\") & FileName.ToLower, FileName)

Then opens it (contained in the class)

Public Sub OpenTempFile(strURL As String, FileName As String)
    Dim req As WebClient = New WebClient()
    Dim response As HttpResponse = HttpContext.Current.Response
    response.Clear()
    response.ClearContent()
    response.ClearHeaders()
    response.Buffer = True
    response.AppendHeader("Content-Disposition", "attachment; filename=""" & FileName & """")
    response.WriteFile(strURL)
    response.Flush()
    response.SuppressContent = True
    HttpContext.Current.ApplicationInstance.CompleteRequest()

This all worked great and passed the proper file path and opened the file (e.g. \\MyServer\Folder...) This was tested both locally and in production and worked as expected.

I had to make a change and pass the file path back to the asp page and then call the procedure to open the file from there.

Class the function from asp page (same)

 Dim ReturnValue As String = OpenMyFile.OpenQCBD(Doc_Id)

Instead of opening the file return the file path

Result = HttpContext.Current.Server.MapPath(fpath & "\") & FileName.ToLower

And then open the file (call from asp page)

OpenMyFile.OpenTempFile(FilePath, Path.GetFileName(FilePath))

This works great running locally on my machine. However when I run it from the production server the class function returns C:Folder/.. instead //server/folder/... like it did before.

Construction of the file path is the same in both scenarios.

OpenTempFile(HttpContext.Current.Server.MapPath(fpath & "\") & FileName.ToLower, FileName)

vs

Result = HttpContext.Current.Server.MapPath(fpath & "\") & FileName.ToLower

The only difference is passing it back to the asp page, this is where I receive the wrong path. Again - works fine on my local machine

Any help or direction would be super helpful, thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source