read csv file in iframe as text file


S

sukhmeet

Hi,
I am trying to load a csv file in Iframe element. I just want to
load it as a text file.
however when i load the file in iframe it asks to open the file and
finally opens it with excel in IE window itself. If i save the save
file as .txt it is being read properly.I need to save the file in csv
format only so that it can be edited externally with excel easily.
Later on i want to display the read csv file in javascript and do some
processing on it.
Does any know how do this. The basic problem is to stop IE from
invoking excel to open csv file and instead read the file as text.

Following code works for .txt file

<iframe name="loader" id="frm" width="1000" height="300"
style="visibility:hidden;display:none" src="Book1.txt"></iframe>

and when i use this line it tries to open the file using excel which i
don't want.

<iframe name="loader" id="frm" width="1000" height="300"
style="visibility:hidden;display:none" src="Book1.csv"></iframe>

Any suggestions?
Thanks
Sukhmeet
 
Ad

Advertisements

B

Bart Van der Donck

  I am trying to load a csv file in Iframe element. I just want to
load it as a text file.
however when i load the file in iframe it asks to open the file and
finally opens it with excel in IE window itself. If i save the save
file as .txt it is being read properly.I need to save the file in csv
format only so that it can be edited externally with excel easily.
Later on i want to display the read csv file in javascript and do some
processing on it.
  Does any know how do this. The basic problem is to stop IE from
invoking excel to open csv file and instead read the file as text.

Following code works for .txt file

<iframe name="loader" id="frm" width="1000" height="300"
style="visibility:hidden;display:none" src="Book1.txt"></iframe>

and when i use this line it tries to open the file using excel which i
don't want.

<iframe name="loader" id="frm" width="1000" height="300"
style="visibility:hidden;display:none" src="Book1.csv"></iframe>

It's always the browser who decides how to open CSV-files. You can try
to influence this behaviour by client- and/or server headers, but
there's no iron guarantee that this will always be successful.

I would use <div> in stead of <iframe>, so it's possible to let
XMLHttpRequest populate the innerHTML of the <div>:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
<html>
<head>
<title>Display CSV in div</title>
<script type="text/javascript">

var xmlhttp;

function loadCSV(url)
{
xmlhttp=null;
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}

function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
var content = xmlhttp.responseText;
content = content.replace(/</g, '&lt;');
content = content.replace(/>/g, '&gt;');
content = content.replace(/\n/g, '<br>');
document.getElementById('frm').innerHTML = content;
}
else
{
alert("Problem retrieving CSV file");
}
}
}
</script>
<style type="text/css">
#frm
{
border: 1px solid black;
font-family: "Courier", "Courier New";
width: 700px;
height: 400px;
}
</style>
</head>

<body onload="loadCSV('Book1.csv?' + new Date().getTime());">
<div id="frm"></div>
</body>

</html>


Note that the Book1.csv call must be randomized by 'new
Date().getTime()' in order to make sure we receive the latest version
of Book1.csv (especially when it's periodically edited in Excel).

Hope this helps,
 
D

Doug Gunnoe

Hi,
  I am trying to load a csv file in Iframe element. I just want to
load it as a text file.
however when i load the file in iframe it asks to open the file and
finally opens it with excel in IE window itself. If i save the save
file as .txt it is being read properly.I need to save the file in csv
format only so that it can be edited externally with excel easily.
Later on i want to display the read csv file in javascript and do some
processing on it.
  Does any know how do this. The basic problem is to stop IE from
invoking excel to open csv file and instead read the file as text.

Following code works for .txt file

<iframe name="loader" id="frm" width="1000" height="300"
style="visibility:hidden;display:none" src="Book1.txt"></iframe>

and when i use this line it tries to open the file using excel which i
don't want.

<iframe name="loader" id="frm" width="1000" height="300"
style="visibility:hidden;display:none" src="Book1.csv"></iframe>

Any suggestions?
  Thanks
Sukhmeet

Bart is correct. However, why not just change the file extension?
Book1.xxx, for example. Or don't use a file extension at all. Later on
why you want to open it up in excel, just go into excel and open the
file. You may have to change the file type on the open file dialogue
window. Just don't allow Excel to create a new file extension
association with 'xxx' or whatever you use.
 
B

Bart Van der Donck

Doug Gunnoe wrote:

[...]
Bart is correct. However, why not just change the file extension?
Book1.xxx, for example. Or don't use a file extension at all. Later on
why you want to open it up in excel, just go into excel and open the
file. You may have to change the file type on the open file dialogue
window. Just don't allow Excel to create a new file extension
association with 'xxx' or whatever you use.

If the objective is that admin edits the CSV-file directly in Excel
(e.g. when opening it using a FTP-path in Excel and saves it back),
then I believe it's better to keep the .csv-extension. This way the
editing can be performed with maximum user-friendliness for admin. The
extension doesn't matter for XMLHttpRequest as it always parses the
raw data.
 
D

Doug Gunnoe

If the objective is that admin edits the CSV-file directly in Excel
(e.g. when opening it using a FTP-path in Excel and saves it back),
then I believe it's better to keep the .csv-extension. This way the
editing can be performed with maximum user-friendliness for admin.

OK. I didn't catch that. I just though the OP wanted to open in it up
in Excel from time to time and edit it, but did not want the browser
to open it up like a spreadsheet.
The extension doesn't matter for XMLHttpRequest as it always parses the
raw data.

But changing the extension keeps IE from associating the file with
Excel and trying to open it up as a spreadsheet in the browser.

But I see your point. It would be a pain if you had to edit the file
frequently or if you had multiple users who needed to edit the file.
 
Ad

Advertisements

D

Doug Gunnoe

If the objective is that admin edits the CSV-file directly in Excel
(e.g. when opening it using a FTP-path in Excel and saves it back),
then I believe it's better to keep the .csv-extension. This way the
editing can be performed with maximum user-friendliness for admin.

OK. I didn't catch that. I just though the OP wanted to open in it up
in Excel from time to time and edit it, but did not want the browser
to open it up like a spreadsheet.
The extension doesn't matter for XMLHttpRequest as it always parses the
raw data.

In regard to the way the OP was doing it, changing the extension keeps
IE from associating the file with Excel and trying to open it up as a
spreadsheet in the browser.

But I see your point. It would be a pain if you had to edit the file
frequently or if you had multiple users who needed to edit the file.

Your solution is the best way.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top