0

I am trying to download a file (example.tif) using a virtual directory (appdata) using Server.MapPath. The virtual directory points to a server separate from my web app's server (\\servername\folder).

This html works and the file downloads:

<a href="appdata/folder/example.tif" target="_blank">Map Link</a>

However I need to use an ASP button / ASPX page and I am writing server side code in VB.

<asp:Button runat="server" OnClick="btnSubmit_ServerClick" OnClientClick="return Validate()" Text="Submit" />

VB Code behind btnSubmit_ServerClick:

Imports System.IO
Imports System.Web.HttpContext

Public Class WebApp
Inherits System.Web.UI.Page

Sub btnSubmit_ServerClick(ByVal source As Object, ByVal e As System.EventArgs)

Dim server As String = Current.Server.MapPath("~/appdata")
Dim fi As String = server & "\\folder\\example.tif"
Dim header As [String] = "Attachment; Filename=example.tif"
Dim dfile As New FileInfo(fi)

Try
        'If file exists download it.
            If File.Exists(fi) Then
                Response.ContentType = "application/octet-stream"
                Response.AppendHeader("Content-Disposition", header)
                Response.WriteFile(dfile.FullName)
                Response.[End]()
            End If

Catch ex As Exception
        lblError.Text = ex.ToString
End Try
End Sub
End Class

I believe the problem to be with how Server.MapPath is reading my virtual directory, as I was able to download the file in Visual Studio while debugging and replacing "server" string to physical path (\servername\folder) I have tried these different symbols and tried taking out the "\" in front of example.tif. The data server needs credentials to access files, so the solution from our central IT guy is to provide the virtual directory. And the HTML link works regardless of permissions, so I don't think its permissions issue. Help appreciated! Thanks!

EDIT: I added the "~" in server.mappath. Fi returns D:Web Files"0appdata\folder\example.tif

Community
  • 1
  • 1
Melolune
  • 21
  • 6

0 Answers0