Click here to Skip to main content
15,914,795 members
Home / Discussions / C#
   

C#

 
QuestionSorting columns in a DataGridView Pin
auloepid31-Jul-07 23:19
auloepid31-Jul-07 23:19 
QuestionSerialize WinForms Pin
SakthiSurya31-Jul-07 23:10
SakthiSurya31-Jul-07 23:10 
AnswerRe: Serialize WinForms [modified] Pin
Martin#1-Aug-07 0:03
Martin#1-Aug-07 0:03 
AnswerRe: Serialize WinForms Pin
Pete O'Hanlon1-Aug-07 1:16
mvePete O'Hanlon1-Aug-07 1:16 
QuestionScreensaver software development Pin
Identity Undisclosed31-Jul-07 22:18
Identity Undisclosed31-Jul-07 22:18 
AnswerRe: Screensaver software development Pin
MarkB77731-Jul-07 23:43
MarkB77731-Jul-07 23:43 
GeneralRe: Screensaver software development Pin
Identity Undisclosed1-Aug-07 0:07
Identity Undisclosed1-Aug-07 0:07 
QuestionDOWNLOAD ERROR - URGENT Pin
RDAAC31-Jul-07 22:18
RDAAC31-Jul-07 22:18 
Hi I am developing an application in ASP.net 2.0 / C# / SQL server2005.The problem is that when end user clicks on download hyperlink it downloads the entire page along with the file which needs to be downloaded.This happens only in case of .HTML extension. e.g. End user wants "a.html" page which is populated on my "xyz.aspx" page.So he click on download link to download "a.html" but ends up downloading "a.html" and "xyz.aspx"(html).

Any help will be greatly appreciated.

Waiting for positive reply.

MY CODE:

DOWNLOAD FUNCTION
private void downloadFile(string sid, string fn,string foldertype)
{
//string siteId = Session["siteid"].ToString();
//string folder = Session["foldertype"].ToString();//

if (File.Exists(Server.MapPath("~/Files/" + foldertype + "/" + sid + "/" + fn)))
{
string filepath = Server.MapPath("~/Files/" + foldertype + "/" + sid + "/" + fn);
if (fn.Substring((fn.Length - 1) - 2, 3) != "pdf")
{
System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[100000];

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;

// Identify the file to download including its path.

Response.Write(filepath);

// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);

try
{
// Open the file.

iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);

// Response .Write(iStream.
// Total bytes to read:
dataToRead = iStream.Length;

Response.Clear();

//if (fn.Substring((fn.Length - 1) - 2, 3) == "pdf")
//{
// Response.ContentType = "application/pdf";
// Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
//}
//else
//{
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
//}

// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);

// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);

// Flush the data to the HTML output.
Response.Flush();

buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write(ex.ToString());
}
finally
{

if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
}
else
{
try
{
Response.Clear();
FileStream MyFileStream = new FileStream(filepath, FileMode.Open);
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.ContentType = "application/pdf";//octet-stream
Response.AddHeader("content-disposition", "attachment; filename=" + fn + "");
Response.BinaryWrite(Buffer);
Response.End();
}
catch (Exception ex1)
{
// Trap the error, if any.
Response.Write(ex1.ToString());
}
}
}
else
{
//lblError.Text = "Temporary file not available";
}

}

HYPERLINK CODE:
protected void filegrid_ItemCommand(object source, DataGridCommandEventArgs e)
{
try
{
MinisiteTableAdapters.FilesTableAdapter ftl = new MinisiteTableAdapters.FilesTableAdapter();
if (e.CommandName == "Download")
{
string folder = e.Item.Cells[11].Text;
downloadFile(e.Item.Cells[7].Text, e.CommandArgument.ToString(), folder);

}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}

RDAAC
QuestionCombo Box (read only) Pin
T.EDY31-Jul-07 22:07
T.EDY31-Jul-07 22:07 
AnswerRe: Combo Box (read only) Pin
il_masacratore31-Jul-07 22:36
il_masacratore31-Jul-07 22:36 
GeneralRe: Combo Box (read only) Pin
T.EDY31-Jul-07 22:39
T.EDY31-Jul-07 22:39 
QuestionWebService in Wireless Network... Pin
Iftekhar Naim31-Jul-07 21:51
Iftekhar Naim31-Jul-07 21:51 
QuestionReuse common code Pin
mpavas31-Jul-07 21:11
mpavas31-Jul-07 21:11 
AnswerRe: Reuse common code Pin
Christian Graus31-Jul-07 21:16
protectorChristian Graus31-Jul-07 21:16 
QuestionWindows Xp Visual Style Pin
Xmen Real 31-Jul-07 21:09
professional Xmen Real 31-Jul-07 21:09 
AnswerRe: Windows Xp Visual Style Pin
Seishin#31-Jul-07 21:34
Seishin#31-Jul-07 21:34 
QuestionCreate a dll for a c# file Pin
mpavas31-Jul-07 21:08
mpavas31-Jul-07 21:08 
AnswerRe: Create a dll for a c# file Pin
Christian Graus31-Jul-07 21:18
protectorChristian Graus31-Jul-07 21:18 
GeneralRe: Create a dll for a c# file Pin
mpavas31-Jul-07 21:21
mpavas31-Jul-07 21:21 
AnswerRe: Create a dll for a c# file Pin
il_masacratore31-Jul-07 21:19
il_masacratore31-Jul-07 21:19 
GeneralRe: Create a dll for a c# file Pin
mpavas31-Jul-07 21:32
mpavas31-Jul-07 21:32 
GeneralRe: Create a dll for a c# file Pin
Christian Graus31-Jul-07 21:59
protectorChristian Graus31-Jul-07 21:59 
GeneralRe: Create a dll for a c# file Pin
PaulPrice31-Jul-07 22:15
PaulPrice31-Jul-07 22:15 
GeneralRe: Create a dll for a c# file Pin
mpavas31-Jul-07 22:45
mpavas31-Jul-07 22:45 
GeneralRe: Create a dll for a c# file Pin
PaulPrice31-Jul-07 22:16
PaulPrice31-Jul-07 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.