[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Force file download / Save information from a page to user?

Posted on 2000-05-09
11
Medium Priority
?
199 Views
Last Modified: 2007-10-18
I've got a spreadsheet in javascript that allows a user to enter some data. I'd like than to give the user a chance to save the data on his local drive in a file.

I thought it's possible to create a stream of non-html mime type thus a browser would offer a user to save the content of the stream to a file, however I haven't succeded.

Bacically, the requirements are:
1) Javascrip/DHTML solution only (in conjunction with a Java applet in the worst case :-)
2) No interaction with a server - everything is client-based.
3) In the end, the information should be save to a file on user's disk, thus the solution of just saving in cokies between the sessions is not good enough.

I would appreciate it very much if anyone has any ideas how to do it.


Thanks,

id
0
Comment
Question by:id
[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
  • 6
  • 4
11 Comments
 
LVL 1

Expert Comment

by:hkmdj
ID: 2792074
If you are using JavaScript, only NN allows for writing to a file on the client machine.  You also must request special priveleges from the browser to do so.

<script language="JavaScript"><!--
function writeToFile(fileName,text) {
 
netscape.security.PrivilegeManager.enablePrivilege('UniversalFileAccess');
    var fileWriter = new java.io.FileWriter(fileName);
    fileWriter.write (text, 0, text.length);
    fileWriter.close();
}

if (document.layers) {
    writeToFile('file.txt','Hello World');
}
//--></script>

dj
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2792272
And here is one for IE

<script>
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
WebBrowser1.ExecWB(4, 0);
WebBrowser1.outerHTML = "";
</script>

It will not create a new mime type.

Michel
0
 

Author Comment

by:id
ID: 2792564
First, thank you both for your replies. It's very helpful!

Michel's reply is more interesting in a short term as my script is curently works with IE only.

Michel, could you please advise me the source of your information about IE extensions like WebBrowser object? The solution you offered is a step in the right direction, but I need a bit more understading of parameters to customise it.

More specifically, the way my spreadsheet works is as follows. A user edits the table, all information is saved in an XML data island. I'd like to bring up Save dialog in my menu Save action and have the XML data to be saved.

Any more ideas in this direction?

Thanks a lot,

id
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 225 total points
ID: 2792726
I have an HTA (html application) that saves an XML file on the user's harddisk
It needs IE5 though and will not bring up the save dialog.


function saveXml() {
   saveFileName  = 'c:\test.xml';
   var oXml = new ActiveXObject("Microsoft.XMLDOM");
   oXml.load(xmlID.XMLDocument); // xmlID is the id of the data island
   if (isError(oXml.parseError)) return;
   try {        
     oXml.save(saveFileName);  // will work in HTA
   }
   catch(e) {
      alert('save:'+e.description);
   }
   alert('File saved as ' + saveFileName);
}


I will have a look for you for the webbrowser object

Michel
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2792837
0
 

Author Comment

by:id
ID: 2792935
Michel,

It's great! We're almost there :-) The solutions sounds as exactly what I need as what I'm creating is effectively an HTA (I would add some dialogs for a user to specify the file name later).

I tried to use it, but I've got some error:
"Objected expected" in the beginning of this line:
var oXml = new ActiveXObject("Microsoft.XMLDOM");

I'm using IE 5.00.2920.0000 on Windows 2000. Do I need to configure/install anything special to be able to use use your example?

Thanks again,

Igor
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2793045
No.

Could it be this line?
oXml.load(xmlID.XMLDocument);

you need to have

<XML ID="xmlID" SRC="myxml.xml"></XML>

or the equivalent for your data island.

I will go home now so I cannot reply unti later this evening (about 5 hours from now)

Michel

0
 

Author Comment

by:id
ID: 2793277
Hmm, should be ...

Here is what I've got:
========================
<HTML>
<HEAD>
<XML id="myXML" src="myXML.xml"></XML>

<SCRIPT>
function saveXML() {
var saveFileName  = 'something.xml';
var oXml = new ActiveXObject("Microsoft.XMLDOM");
oXml.load(myXML.XMLDocument); // xmlID is the id of the data island
if (isError(oXml.parseError)) return;
   try {          
     oXml.save(saveFileName);  // will work in HTA
   }
   catch(e) {
      alert('save:'+e.description);
   }
   alert('File saved as ' + saveFileName);
}
</SCRIPT>
</HEAD>
test
<SCRIPT>
  saveXML();
</SCRIPT>
</HTML>
==============
It reports "Object expected" on line: if (isError(oXml.parseError)) return;

If I comment this line out the script works, but then I catch a "Permission denied" error message on save.

Any more ideas? ;-)

I really appreciate your help!

Thanks,

id
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2794260
Sorry. my fault.

isError is of course a function I wrote...

I do not have it here - al it does is format the error message

The island should NOT be in the head!!!

and it is remotely possible you may not save the file unless you ower the security or make  real HTA out of it

Michel

0
 

Author Comment

by:id
ID: 2796439
1) isError - mea culpa, must've spotted it :-)

2) I think it was indeed a security problem as the only way to get this code working I've found is to make it a real HTA, which is acceptable in my case I think (though I haven't had much experience with HTA(haven't heard about it before yesterday :-), so I'm not sure if there are any problems with using it that could crop up in future). In general HTA doesn't seem to be a very popular technology, does it? I haven't found much information on it on MSDN(though found some general articles). Could you recommend any other resources to look at?

3) Although I can save the data now, it has a major drawback now that I can give a nice "Save As" dialog to the user, so he can choose where to save it to. Is there any ActiveX control I can use like FileChooser in Java?

4) Lastly ;-) Why couldn't I have XML data island in the head? It works for me both ways, so any particular reason?

I know it's grown in a quite a big discussion, but I'd be extremely grateful if you'd complete the tutorial :-)

Cheers,

Igor
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 2797602
Only valid tags in the head is
style
meta
title
script

The HTA is so new that it is not popular yet. I didn't know about it until I had to come up with a way to get 12 eastern european countries to translate the contents of an html file without  messing the format up.
I found that XML and an hta with some clever xsl formatting did the job just nicely thank you...

Here is my isError:

function isError(xPE) {
   if (xPE.errorCode > 0) {
      var strErrText = 'The XML Document failed to load due the following error:';
      with (xPE) {
            strErrText += "\nError #         : " +errorCode +": " + reason;
            strErrText += "\nLine #          : " + line;
            strErrText += "\nLine Position   : " + linepos;
            strErrText += "\nPosition In File: " + filepos;
            strErrText += "\nSource Text     : " + srcText;
            strErrText += "\nDocument URL    : " + url;
      }
      alert(strErrText);
      return true; // isError;
   }
   else return false;
}


To give a save as, you might cheat with a
<form name=myForm oSubmit="return false">
<input type="file" name=myFile>
</form>
and use document.myForm.myFile.value


Michel
PS: I will not be bck until Tuesday. I hope you have solved it by then
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

649 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