Click here to Skip to main content
15,074,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing an intranet web application to enable users to store and download files from the servers local hard drive. I am presenting users with a jquery treeview to select the files from. The treeview returns the full absolute path of the file. This all works fine, my issue is as follows.

I cannot seem to find a way to enable the users to download the files from there. I have tried the following:

In Javascript
window.open('file:///c://Documents/folder/file.ext', 'Download');


In HTML
<a href="file:///C://Documents/folder/file.ext">Download Me</a>


Neither of these do anything. If I enter the address provided in either of those instances directly into the url of my browser, I get the desired effect. Please assist me in finding a way to achieve this goal in the click event of my javascript.

Thanks
Posted
Updated 22-Oct-10 6:30am
v2
Comments
William Winner 22-Oct-10 12:11pm
   
that works for you because I assume you are accessing the link from the server itself. file:/// tells the browser to look on the local computer for the file. So if you use a browser on your server, it looks on the servers hard-drive.

However, if Joe Schmo uses his computer (not the server) and types that into his browser, it will look for that file on Joe Schmo's computer.

Okay, well I guess I've taken the incorrect approach entirely. My intention was to enable the user to download the file from the servers hard-drive. My treeview contains the current file structure for a directory on the server, and I would like to enable users to download any file present.

Currently, when a user selects a file, a javascript event is fired. My goal is to use this javascript event to open the download for the user. My function is filled with the parameter of the local address to the server. So if I have this local address (on the servers hard-drive), how can I enable the user to download this file (to their local hard-drive)?


My basic server folder structure looks like.
Documents
TreeViewDirectory
WWW Directory (Where site is hosted)

And I have a javascript function on the page collecting data from a post from which I want to enable the user to download the file.
function(serverFileAddress)<br />
{<br />
//currently its <br />
window.open(serverFileAddress, 'Download');<br />
<br />
//what should it be?<br />
}<br />


serverFileAddress will return the proper download if navigated to through the url address.

Thank you.
   
You need the opposite of Server.MapPath like this:

VB
Public Function MapURL(ByVal Path As String) As String
     Dim AppPath As String = _
     HttpContext.Current.Server.MapPath("~")
     Dim url As String = String.Format("~{0}" _
     , Path.Replace(AppPath, "").Replace("\", "/"))
     Return url
End Function

Source: http://geekswithblogs.net/AlsLog/archive/2006/08/03/87032.aspx[^]

This will translate the file path to an url that will work for your users.

Good luck!
   
Thanks for the answer E.F., but I don't think that is a solution that will work for me. That method seemed to simply return the location of the application + the path parameter:

i.e.: C:\\Documents\\usr\\www Folder\\Current Application\\ + filled parameter

But in this case, my parameter is actually the full physical address for the file on the servers hard-drive. My post returns something similar to:

C:\\Documents\\usr\\File Structure\\Treeview Directory\\filename.ext

I already have the exact address on the physical hard-drive. Now I need to use that to provide a download from a javascript method. I believe part of my issue is related to the fact that the treeview structure doesn't reside in the application, it sits higher in my file structure.

C:\\
::Documents
:::USR
::::File Structure
:::::Treeview Directory
::::::File1.ext
::::::File2.ext
::::::File3.ext
::::WWW Folder
:::::Current Application Folder

I really want to avoid having the treeview folder structure under the application. This file structure is used for a good deal of other activities, and would like to maintain its independence.

Is there a way to link to this file? When I try to link directly to the file i.e. (|a href="c://documents//usr//file structure//treeview directory//file1.ext"|DL|/a|) i get an error that the file is unable to be opened because the file isn't associated with any program

This seems like it should be so simple, but yet I've been baffled by it for hours. I apologize for my ignorance, any assistance is greatly appreciated. Thanks.
   

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