1

I want to force user to download a file. My website folder path is D:\websites\domain\ and file path is E:\folder\file.bak,

ASP - VBScript

<%

   Dim Stream
   Dim Contents
   Dim FileName
   FileName = "E:\db\A101.bak"
   Response.ContentType = "application/octet-stream"
   Response.AddHeader "content-disposition", "attachment; filename=" & FileName
   Set Stream = server.CreateObject("ADODB.Stream")
   Stream.Open
   Stream.LoadFromFile Server.MapPath(FileName)
   Contents = Stream.ReadText
   Response.BinaryWrite Contents
   Stream.Close
   Set Stream = Nothing

%>

its working perfectly if file is inside my website folder, But if i use the path E:\db\A101.bak give me an error, File not Found. i can't move file inside the Website folder due to security reasons. Help me

pedrofurla
  • 12,763
  • 1
  • 38
  • 49
lucky Aman
  • 74
  • 2
  • 6
  • 1
    Why are you trying to act like malware? – Ken White Oct 07 '12 at 05:39
  • Web servers aren't supposed to give access to anything outside the website folder. This behavior is by design. Please take a step back and describe the problem you're trying to solve instead of what you perceive as the solution. – Ansgar Wiechers Oct 07 '12 at 09:44
  • 3
    What's wrong with what he is doing? He is loading a specific file and sending it to the client. I presume there is some reason behind this and there may be some security to allow only specific access. If the file was in the website folder then potentially anyone could retrieve the file. Usually problems like this are that the user that the website is running under doesn't have permission to access this folder, but not if you are getting a file not found error. – johna Oct 08 '12 at 03:29
  • If this is just a case of serving a file that's outside the websites directory – another option available to you is IIS virtual directories: http://technet.microsoft.com/en-us/library/cc771804(v=ws.10).aspx - you can map the virtual folder with read-only credentails etc – HeavenCore Oct 08 '12 at 09:27

1 Answers1

2

Since you already know your physical path you don't need the Server.MapPath method. (This method is normally used to translate a virtual path to a physical path) The Server.MapPath method probably does not work for a path outside the website's structure...

So, try using

Stream.LoadFromFile FileName
AardVark71
  • 3,928
  • 2
  • 30
  • 50
  • Quick check on related SO posts gave me this post which gives additional explanation : http://stackoverflow.com/questions/3422270/how-to-use-server-mappath-to-get-location-outside-website-folder-in-asp-net – AardVark71 Oct 08 '12 at 08:16