Home > Archive > MS SQL Server > March 2006 > User Downloading files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author User Downloading files
sorourke1@hotmail.com

2006-03-05, 8:23 pm

I have a web application that allows the user to download a file (CSV). The
file actually downloads a csv file that contains the html for the page. so if
I open the downloaded file in Internet Explorer, my ASP page comes up below
the csv text. I have pasted some codes below. What am I missing? I think it
has comething to do with transfering the data via Byte.

//Create the temp file path
//string strFileName = @"C:\Temp\BPM_Export.csv";
string strFileName = "BPM_Export.csv";
FileInfo fiBPM_Export = new FileInfo(strFileName
);

// Delete the file if it exists.
if (fiBPM_Export.Exists)
{
fiBPM_Export.Delete();
}

//Create the file.
using (FileStream fsBPM_Export = fiBPM_Export.Create())
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text
in the file.");

//Add the information to the file.
fsBPM_Export.Write(info, 0, info.Length);
}

//Open the stream and read it back.
using (StreamReader sr = fiBPM_Export.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}

//set the conttent type of the file to be downloaded
Response.ContentType = "APPLICATION/OCTET-STREAM";
// initialize the http content-disposition header to
// indicate a file attachment with the filename
//Response.AppendHeader("Content-Disposition", "Attachment;
Filename=\"" + strFileName + "\"");
Response.AddHeader("Content-Disposition", "Attachment; Filename=\""
+ strFileName + "\"");
//// transfer the file byte-by-byte to the response object
FileInfo fileToDownload = new FileInfo(strFileName
);
Response.Flush();
Response. WriteFile(fileToDown
load.FullName);

//close the connection
cmdBPMExport.Connection.Close();
//Delete the Temp File
fiBPM_Export.Delete();
sorourke1@hotmail.com

2006-03-05, 8:24 pm

Thanks!

"shriop" wrote:

> Response.Clear();
> Response.ClearHeaders();
>
> then write the file...
>
> Response.End();
>
> The mime type that you're using also seems odd. I tend to use
> "application/vnd.ms-excel".
>
> Bruce Dunwiddie
> http://www.csvreader.com
>
> sorourke1@hotmail.com wrote:
>
>

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com