file download question in C# and asp.net

A

Anil S

Hello,

When I try to download a file from asp.net (C#) application using 3.5 framework, somehow it also includes html codes in that file. Can you tell me how to get the file without html codes.

Thank you

Here is my code:
Response.Clear();
string FilePath = System.Configuration.ConfigurationManager.AppSettings["SourceFiles"].ToString();
string sourcepath1 = FilePath + "CompleteReport.csv";
string FileName1 = System.IO.Path.GetFileName(sourcepath1);
Response.AppendHeader("content-disposition", "attachment; filename=" + FileName1);
Response.ContentType = "application/vnd.ms-excel";
Response.TransmitFile(sourcepath1);



Here is what I should see in the downloaded file:

source,type ,name,pnodeId ,timestamp,LMP ,EnergyComponent,ScarcityInEffect,mccValue ,mlcValue
------,-------------------------,----,-----------,---------,------------,---------------,----------------,------------,------------

(0 rows affected)



Here is what I see in the downloaded file:



source,type ,name,pnodeId ,timestamp,LMP ,EnergyComponent,ScarcityInEffect,mccValue ,mlcValue
------,-------------------------,----,-----------,---------,------------,---------------,----------------,------------,------------

(0 rows affected)


<!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>
Windy Bay Power: We Trade Electricity!
</title>
<link href="App_Themes/Default/WPPIT.css" type="text/css" rel="stylesheet" /></head>
<body>
<form name="aspnetForm" method="post" action="LMPReport.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE5ODY3MjM5MTEPZBYCZg9kFgICAw9kFgICAw9kFgICAQ9kFg4CBg8QDxYCHgtfIURhdGFCb3VuZGdkEBUDBklTTy1ORQZOWUlTTyAGUEpNICAgFQMBNAExATIUKwMDZ2dnFgECAWQCCg8QDxYCHwBnZBAVAwlH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gDBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WBQUjY3RsMDAkTG9naW5WaWV3MSRMb2dpblN0YXR1czEkY3RsMDEFI2N0bDAwJExvZ2luVmlldzEkTG9naW5TdGF0dXMxJGN0bDAzBS9jdGwwMCRDb250ZW50UGxhY2VIb2xkZXIxJExNUFJlcG9ydDEkTGlzdEJveEFkZAUyY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRMTVBSZXBvcnQxJExpc3RCb3hSZW1vdmUFLWN0bDAwJENvbnRlbnRQbGFjZUhvbGRlcjEkTE1QUmVwb3J0MSRkb3dubG9hZAUpY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRMTVBSZXBvcnQxJGdyaWQPPCsACgEIAgFkBRBjdGwwMCRMb2dpblZpZXcxDw9kAgNkuPbfB70vLPOb8NwQKMu2sIuloEY=" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>


<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWPQL8jJH1CgKRuOTfBQLw/c6RBwL8kuT/CwL/kuT/CwL+kuT/CwLo5saMCgLmieziBgLlieziBgLnieziBgKcgrJdAq6Czl0CnoGaRgLOvuiUBwKMpJTrBgLf/cKiCgKu/pqtAQKIvI/8CgKIvKPBAQKIvJf4DAKIvIvdCwLvpZW0CQLvpYkZAu+lveIPAu+l0ccGAu+lxagOAu+l+Y0FAu+l7dYMAu+lgboEAu+l9dIGAu+l6bcOAsKO+64DAsKO7/MKAsKOg9cBAsKOt7gJAsKOqx0Cwo7f5g8Cwo7zywYCwo7nrA4Cwo7bxwgCwo7PKALp5LvvCgLp5K+wAgLp5MOVCQLp5Pd+Aunk68MPAunkn6cHAunks4gOAunkp+0FAunkmwQC6eSP6Q8CzM2ZwAwCzM2NpQQCzM2hjgMCzM3V0woCzM3JtAICzM39mQkCzM2RfQLMzYXGDwLy+8/1BQLd3rLxC0zfTPZXFmRc17PG9/nmSnQaGsw5" />
</div>




<span id="ctl00_ContentPlaceHolder1_LMPReport1_heading" class="CatalogTitle">LMP Report</span><br />
<span id="ctl00_ContentPlaceHolder1_LMPReport1_statusLabel" style="color:Red;"></span><br />
<table>
<tr><td><span id="ctl00_ContentPlaceHolder1_LMPReport1_SourceLabel">*Select An Source:</span></td>
<td><select name="ctl00$ContentPlaceHolder1$LMPReport1$ddlSource" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$LMPReport1$ddlSource\',\'\')', 0)" id="ctl00_ContentPlaceHolder1_LMPReport1_ddlSource" style="width:220px;">
<option value="4">ISO-NE</option>
<option selected="selected" value="1">NYISO </option>
<option value="2">PJM </option>

</select> </td>
</tr>
<tr><td><span id="ctl00_ContentPlaceHolder1_LMPReport1_TypeLabel">*Select A Type:</span></td>
<td><select name="ctl00$ContentPlaceHolder1$LMPReport1$ddlType" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$LMPReport1$ddlType\',\'\')', 0)" id="ctl00_ContentPlaceHolder1_LMPReport1_ddlType" style="width:220px;">
<option value="2">Generator</option>
<option selected="selected" value="3">ReferenceBus</option>
<option value="1">Zone</option>

</select> </td>
</tr>
<tr><td><span id="ctl00_ContentPlaceHolder1_LMPReport1_DataTypeLabel">*Data Type:</span></td>
<td><select name="ctl00$ContentPlaceHolder1$LMPReport1$ddlDataType" id="ctl00_ContentPlaceHolder1_LMPReport1_ddlDataType" style="width:220px;">
<option selected="selected" value="DA">DAY AHEAD </option>
<option value="RT">REAL TIME </option>

</select> </td>
</tr>
<tr>
<td align="left" valign="top"><span id="ctl00_ContentPlaceHolder1_LMPReport1_Label2">Add Point(s):</span></td>
<td align="left" valign="top"><select size="10" name="ctl00$ContentPlaceHolder1$LMPReport1$ListBoxAdd" multiple="multiple" id="ctl00_ContentPlaceHolder1_LMPReport1_ListBoxAdd" style="width:220px;">

</select></td>
<td align="left" valign="top"><input type="submit" name="ctl00$ContentPlaceHolder1$LMPReport1$addPoint" value="Add PointID" id="ctl00_ContentPlaceHolder1_LMPReport1_addPoint" class="SmallButtonText" /></td>
</tr>
<tr>
<td align="left" valign="top"><span id="ctl00_ContentPlaceHolder1_LMPReport1_Label3">Remove Point(s):</span></td>
<td align="left" valign="top"><select size="10" name="ctl00$ContentPlaceHolder1$LMPReport1$ListBoxRemove" multiple="multiple" id="ctl00_ContentPlaceHolder1_LMPReport1_ListBoxRemove" style="width:220px;">
<option value="9407">NYISO_LBMP_REFERENCE</option>

</select></td>
<td align="left" valign="top"><input type="submit" name="ctl00$ContentPlaceHolder1$LMPReport1$RemovePoint" value="Remove PointID" id="ctl00_ContentPlaceHolder1_LMPReport1_RemovePoint" class="SmallButtonText" /></td>
</tr>
<tr><td align="left" valign="top">
<span id="ctl00_ContentPlaceHolder1_LMPReport1_Label1">*Select A Date:</span></td><td>
<table id="ctl00_ContentPlaceHolder1_LMPReport1_Calendar1" cellspacing="0" cellpadding="2" title="Calendar" border="0" style="border-width:1px;border-style:solid;border-collapse:collapse;">
<tr><td colspan="7" style="background-color:Silver;"><table cellspacing="0" border="0" style="width:100%;border-collapse:collapse;">
<tr><td style="width:15%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','V3227')" style="color:Black" title="Go to the previous month">&lt;</a></td><td align="center" style="width:70%;">December 2008</td><td align="right" style="width:15%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','V3288')" style="color:Black" title="Go to the next month">&gt;</a></td></tr>
</table></td></tr><tr><th align="center" abbr="Sunday" scope="col" style="color:BlueViolet;">Sun</th><th align="center" abbr="Monday" scope="col" style="color:BlueViolet;">Mon</th><th align="center" abbr="Tuesday" scope="col" style="color:BlueViolet;">Tue</th><th align="center" abbr="Wednesday" scope="col" style="color:BlueViolet;">Wed</th><th align="center" abbr="Thursday" scope="col" style="color:BlueViolet;">Thu</th><th align="center" abbr="Friday" scope="col" style="color:BlueViolet;">Fri</th><th align="center" abbr="Saturday" scope="col" style="color:BlueViolet;">Sat</th></tr><tr><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3256')" style="color:Black" title="November 30">30</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3257')" style="color:Black" title="December 01">1</a></td><td align="center" style="color:White;background-color:Silver;width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3258')" style="color:White" title="December 02">2</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3259')" style="color:Black" title="December 03">3</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3260')" style="color:Black" title="December 04">4</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3261')" style="color:Black" title="December 05">5</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3262')" style="color:Black" title="December 06">6</a></td></tr><tr><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3263')" style="color:Black" title="December 07">7</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3264')" style="color:Black" title="December 08">8</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3265')" style="color:Black" title="December 09">9</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3266')" style="color:Black" title="December 10">10</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3267')" style="color:Black" title="December 11">11</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3268')" style="color:Black" title="December 12">12</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3269')" style="color:Black" title="December 13">13</a></td></tr><tr><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3270')" style="color:Black" title="December 14">14</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3271')" style="color:Black" title="December 15">15</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3272')" style="color:Black" title="December 16">16</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3273')" style="color:Black" title="December 17">17</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3274')" style="color:Black" title="December 18">18</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3275')" style="color:Black" title="December 19">19</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3276')" style="color:Black" title="December 20">20</a></td></tr><tr><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3277')" style="color:Black" title="December 21">21</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3278')" style="color:Black" title="December 22">22</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3279')" style="color:Black" title="December 23">23</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3280')" style="color:Black" title="December 24">24</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3281')" style="color:Black" title="December 25">25</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3282')" style="color:Black" title="December 26">26</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3283')" style="color:Black" title="December 27">27</a></td></tr><tr><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3284')" style="color:Black" title="December 28">28</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3285')" style="color:Black" title="December 29">29</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3286')" style="color:Black" title="December 30">30</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3287')" style="color:Black" title="December 31">31</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3288')" style="color:Black" title="January 01">1</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3289')" style="color:Black" title="January 02">2</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3290')" style="color:Black" title="January 03">3</a></td></tr><tr><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3291')" style="color:Black" title="January 04">4</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3292')" style="color:Black" title="January 05">5</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3293')" style="color:Black" title="January 06">6</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3294')" style="color:Black" title="January 07">7</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3295')" style="color:Black" title="January 08">8</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3296')" style="color:Black" title="January 09">9</a></td><td align="center" style="width:14%;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LMPReport1$Calendar1','3297')" style="color:Black" title="January 10">10</a></td></tr>
</table>


</td></tr>
<tr><td>
<input type="submit" name="ctl00$ContentPlaceHolder1$LMPReport1$GetReport" value="Get Report" id="ctl00_ContentPlaceHolder1_LMPReport1_GetReport" class="SmallButtonText" /></td><td></td></tr>
</table>
<br />
<input type="image" name="ctl00$ContentPlaceHolder1$LMPReport1$download" id="ctl00_ContentPlaceHolder1_LMPReport1_download" src="Images/download.png" style="border-width:0px;" />
<br />
<div>
<table cellspacing="1" cellpadding="1" rules="all" border="1" id="ctl00_ContentPlaceHolder1_LMPReport1_grid">
<tr>
<th scope="col">Name</th><th scope="col">00</th><th scope="col">100</th><th scope="col">200</th><th scope="col">300</th><th scope="col">400</th><th scope="col">500</th><th scope="col">600</th><th scope="col">700</th><th scope="col">800</th><th scope="col">900</th><th scope="col">1000</th><th scope="col">1100</th><th scope="col">1200</th><th scope="col">1300</th><th scope="col">1400</th><th scope="col">1500</th><th scope="col">1600</th><th scope="col">1700</th><th scope="col">1800</th><th scope="col">1900</th><th scope="col">2000</th><th scope="col">2100</th><th scope="col">2200</th><th scope="col">2300</th><th scope="col">Avg</th><th scope="col">Peak_Avg</th><th scope="col">OffPeak_Avg</th>
</tr><tr>
<td>NYISO_LBMP_REFERENCE</td><td>$53.07</td><td>$40.73</td><td>$40.49</td><td>$39.57</td><td>$39.53</td><td>$49.79</td><td>$59.89</td><td>$64.92</td><td>$70.19</td><td>$69.03</td><td>$69.88</td><td>$69.80</td><td>$64.78</td><td>$64.66</td><td>$61.81</td><td>$62.45</td><td>$70.43</td><td>$87.76</td><td>$74.19</td><td>$75.90</td><td>$70.42</td><td>$67.56</td><td>$58.44</td><td>$60.36</td><td>$61.90</td><td>$68.60</td><td>$48.50</td>
</tr>
</table>
</div>





</form>

</body>
</html>
 
M

Martin Honnen

Anil said:
Hello,

When I try to download a file from asp.net (C#) application using 3.5 framework, somehow it also includes html codes in that file. Can you tell me how to get the file without html codes.

Thank you

Here is my code:
Response.Clear();
string FilePath = System.Configuration.ConfigurationManager.AppSettings["SourceFiles"].ToString();
string sourcepath1 = FilePath + "CompleteReport.csv";
string FileName1 = System.IO.Path.GetFileName(sourcepath1);
Response.AppendHeader("content-disposition", "attachment; filename=" + FileName1);
Response.ContentType = "application/vnd.ms-excel";
Response.TransmitFile(sourcepath1);

Try whether
Response.End();
here fixes the problem.
 
M

Moe Sisko

Hello,

When I try to download a file from asp.net (C#) application using 3.5
framework, somehow it also includes html codes in that file. Can you tell me
how to get the file without html codes.
Can also try adding something like :

int fileSize = .. get number of bytes in file
Response.AddHeader ("Content-Length", ... fileSize.ToString() );
 

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