Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Downloading File through java script on client browser!

Posted on 2010-08-23
8
Medium Priority
?
696 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:yarabati
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 5

Expert Comment

by:Ultra_Master
ID: 33508158
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

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33509015
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.
0
 

Author Comment

by:yarabati
ID: 33509026
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:Ultra_Master
ID: 33509493
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
0
 
LVL 5

Accepted Solution

by:
Ultra_Master earned 1000 total points
ID: 33509534
One more thing,

If you create the folder tests on your localhost server and copy the html source file and the xsl file in that folder, the url would be: http://localhost/tests/index.html (or whatever the file name . html)

An alias to the localhost is the IP: 127.0.0.1 -> http://127.0.0.1/tests/index.html
0
 

Author Comment

by:yarabati
ID: 33509763
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.
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 1000 total points
ID: 33509773
0
 

Author Comment

by:yarabati
ID: 33510806
Great worked like charm even though it took time to me to understand...

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question