[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

How to Invoke 'Save As' Dialog Box on page created by javascript

The Function below loops through html table data and creates what looks like essentially a .csv file.  How could I change this code so that it...

A)  Writes the data to a new tab instead of overwriting the original page

B)  Automatically forces a 'save as' dialog box so that a user can save the content as a .txt file on  their computer
function convertCSV(){
 
var r  = 0;
MyTable = document.getElementById('tablesort');
csvout = window.document ;
numofRows = MyTable.rows.length-1;
numofCells =  MyTable.rows[0].cells.length-1;    //
var rowcsv= [numofRows];
var cntrows = 0;
for ( r == 0; r <= numofRows; r++) {
     var c =0;
     tempdata = "";
     for (c == 0; c<=numofCells; c++) {
 
		if (c != numofCells && MyTable.rows[r].cells[c].style.display!='none') {
             tempdata+= MyTable.rows[r].cells[c].innerHTML + ",";
            }else{
				if(c != numofCells && MyTable.rows[r].cells[c].style.display=='none'){
				}else{
					tempdata+= MyTable.rows[r].cells[c].innerHTML= "<br>";
					
			 }
 
          }
	    }
     rowcsv[r] = tempdata
     }
csvout.open("text/html");
var rowcnt = 0;
for (rowcnt == 0; rowcnt<= rowcsv.length-1; rowcnt++){
    csvout.write(rowcsv[rowcnt]);
    } csvout.close();
}

Open in new window

0
drmans
Asked:
drmans
  • 4
  • 2
1 Solution
 
rdivilbissCommented:
It is a browser security violation to attemp to invoke File, Save As from within your web page.  So this isn't possible using JavaScript, nor should it be.

However, you still have the option of serving your results as a file download the user can accept or reject by using the correct content disposition header in your page.

Content-disposition: attachment; filename=fname.ext
0
 
rdivilbissCommented:
If you use the FSO to create a temp file on your server of the results then you could use this code:
<%
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition","attachment; filename=test.csv"
 
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
 
'Open a stream
objStream.Type = 2 ' adTypeText
objStream.Open
objStream.Charset = "iso-8859-1"
objStream.LoadFromFile Server.MapPath("/database/test.txt")
 
' objStream.Position=0
Response.write objStream.ReadText
objStream.Flush
objStream.Close
%>

Open in new window

0
 
rdivilbissCommented:
This might be more useful to your situation.
<%
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
 
objStream.Type = 2 ' adTypeText
objStream.Open
 
 
'%%%% create your data and append it to the stream instead of tempdata
 
objStream.WriteText "whatever"
'repeat as necessary
 
 
 
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition","attachment; filename=test.csv"
objStream.Position=0
Response.write objStream.ReadText
objStream.Flush
objStream.Close
	
%>

Open in new window

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.

 
drmansAuthor Commented:
That makes sense...
In regards to this code --> Content-disposition: attachment; filename=fname.ext
Is there any way to create the filename automatically by pulling information from the url and then setting the default extension to .txt?
0
 
drmansAuthor Commented:
Ok...so ultimately if i were to use the 'objStream.WriteText "whatever"' method that you suggest is it possible to have the javascript function automatically dump the output into "whatever".  Or else how would I append the dynamically created data to the stream?
0
 
rdivilbissCommented:
>In regards to this code --> Content-disposition: attachment; filename=fname.ext
Is there any way to create the filename automatically by pulling information from the url and then setting the default extension to .txt?

You'll need to use ASP.  JavaScript won't begin until after the header is written.

Create the tempdata on one page (don't write to the browser), post the tempdata string to a file download web page, retrieve it with Request.Post and do your stream on that page.  The only thing the user will note is a change of URL (maybe - depends on how fast your first page executes.)  Or simpler, redo the JS part in ASP.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now