drmans
asked on
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
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();
}
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
%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
ASKER
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?
>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.
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.
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