Link to home
Start Free TrialLog in
Avatar of yarabati
yarabatiFlag for India

asked on

Downloading File through java script on client browser!

Hi experts...

Hope you all are doing good !!!

Well, I am newbie to this java script coding. I need to implement file download functionality through java script in my xhtml page.

Below is the script written:
		    function jsexportXLS(){
			    alert("about to download...");
		    	window.open("D:\Shashi\MyFile.xls");
		    	alert("window opened...");
			}

Open in new window


I am able to see alert message before opening new window. But after that I see in java script error in my browser (IE). error is "Access is denied".
But I clearly see there is no restrictions on my excel page.

Can any one help me any other way to download a file through java script if I am wrong in above?
Also when we say the file name, does it append to end of url ? I guess it should be like that...  Any help is appreciated...

Cheers,
yarabati
Avatar of Ultra_Master
Ultra_Master

Try to use the server path and not the path on the hdd.

If you use your localhost as the server and you have a folder named tests then copy the xls file in the tests folder and use the url to open the file (aka to invoke the file download)

Check the code below.

Cheers,
Ultra_Master
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<script type="text/javascript">
function jsexportXLS(){
			    alert("about to download...");
		    	window.open("http://localhost/tests/MyFile.xls");
		    	alert("window opened...");
			}
</script>

<body>

<input type="button" value="Download" onclick="jsexportXLS()" />
</body>
</html>

Open in new window

Avatar of Michel Plungjan
A web page on a server will not be allowed to open a file on the client without being very clearly allowed by the user. HTA/WSH using the file system object for example.

You can access the file using a file upload (<input type="file") , but you cannot fill in the field as a web developer.
Avatar of yarabati

ASKER

Hi Ultra master

thank you very much for helping me out.

I see i have 2 problems here.

1. With the above code I am getting script error in browser "Access is denied". but I don't have restrictions on xls file. This file is created by my program by Apache POI library.

2. Could you please be more specific where can have tests folder inside my JBoss folder structure?
Well, first of all you cannot access a file stored on your computer or a client computer by using its operating system access path i.e. D:\Shashi\MyFile.xls in your case, no matter whether you are the owner of that file or that no restriction is applied to it. This is a default security measure for JavaScript behavior in order for malicious users not to tamper with the files on the users' computers via bad intended JavaScript coding. Therefore security measures were imposed that do not allow using this behavior.

Anyway, you can use this approach if you target the file via it's URL, if the files resides on a server.

You can also test it on you own server.

If you don't have one, simply go here (  http://www.easyphp.org/   ) and download easyphp then install it and go to the folder named www and there's your localhost root folder.

You simply place the html file on the root folder of your local server together with your xls file and then you go to your browser (Internet Explorer, Opera, Firefox, etc) and simply type the url which is:
http://localhost/your_html _file.html and press enter. If everything went well you should be able to see the Download button from my code and download the xsl file.

In other words you need a server where to run the code. The server (installed via easyphp) acts like a real web server and you can also test php code on it.

Good luck,
Ultra_Master
ASKER CERTIFIED SOLUTION
Avatar of Ultra_Master
Ultra_Master

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, that's the brief explanation to understand.

I am using JBoss Server. Now I need to test this by placing the excel file in appropriate server folder.
Let me check this.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great worked like charm even though it took time to me to understand...

Thanks all of you who helped me! Have a great day.