Reputation: 423
I am generating a string with comma separated variables and trying to export that. The problem is that the whole .html page is getting appended alongwith the exported .csv file.
I have two instances of code, one in local server and other in testing server. I am using the same database in both, the codebase is same in both still, there is an issue while exporting the .csv file.
Following is the code I am using for exporting...
//name - Name of File to save
//text - Comma separated string
private void ExportToCSV(string fileName, string text)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
if (text != null && text.Length > 0)
{
Response.AddHeader("Content-Length", text.Length.ToString());
Response.Output.Write(text);
}
}
I have tried using Response.Write(text)
, but still having the same issue.
This is the output I am getting when I get the exported .csv file...
SEARCH DATE,USER TYPE,CITY
20-08-2015,TM,Chicago
20-08-2015,TM,New York
20-08-2015,TM,London
20-08-2015,TM,London
20-08-2015,TM,London
20-08-2015,TM,Cape Town
20-08-2015,TM,Mumbai
20-08-2015,TM,Ottawa
20-08-2015,TM,Mumbai
20-08-2015,TM,Istanbul
<!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><title>
Search Report
</title>
<script src="Jquery/jquery-1.9.1.js" type="text/javascript"></script>
<script src="Jquery/jquery-ui-1.9.2.custom/js/jquery-ui-1.9.2.custom.js"
type="text/javascript"></script>
<script type="text/javascript">
var t = false;
window.alert = function(message) {
$(document.createElement('div'))
.attr({ title: 'Alert', 'class': 'alert' })
.html(message)
.dialog({
buttons: { OK: function() { $(this).dialog('close'); } },
close: function() { $(this).remove(); },
draggable: true,
modal: true,
resizable: false,
width: 'auto'
});
};
$(function() {
var useragent = navigator.userAgent;
var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;
var pos = useragent.indexOf('MSIE');
var bVer = "";
if (pos > -1) {
$('li').has('ul').mouseover(function() {
$(this).children('ul').show();
}).mouseout(function() {
$(this).children('ul').hide();
})
}
});
</script>
.
.
.
.
.
.
</div>
</div>
</body>
</html>
I am unable to figure out the root cause of this issue due to which this is happening. If someone can explain the reason along with the solution, it would be of immense help. Thanks.
Upvotes: 0
Views: 202
Reputation: 45967
you just need to add
Response.End();
i would also add Response.ContentType = "text/csv";
for the correct recognition by the browser
Upvotes: 2