Link to home
Create AccountLog in
Avatar of CompuFit
CompuFit

asked on

Downloading generated text file

Hey,

I have this code. The code will generate an text file with a special extension (.o2l) under a temp directory from the website. The file is generated without a problem (example name <myFile.o2l>).
Then i send the file to the browser with the following line:
FileHandler.ResponseOpenFile(pathFile, "text/plain", fileInfo.Name, Response);

I always want to download the text file to the hard disk.
In IE:
I get a pop up for downloading the file.
Open file => the text in show in the browser
Save as => the windows just closes, no file is saved on my hard drive

In Firefox:
I get the pop up for downloading the file
I save the file on my desktop => myFile (but without an extension)

i need to fix these problems:
IE => download to disk
Firefox => save with extension

Can somebody help me?
protected void btnDownload_Click(object sender, EventArgs e)
{
string pathFile = Compufit.iSecurity.Encrypt.XML.ExportXML(sI, tmpPath, false, Compufit.iSecurity.Encrypt.EMsgType.None, null);
                            System.IO.FileInfo fileInfo = new System.IO.FileInfo(pathFile);
                            if (fileInfo.Exists)
                            {
                                FileHandler.ResponseOpenFile(pathFile, "text/plain", fileInfo.Name, Response);
                            } 
}
 
public static class FileHandler
    {
        /// <summary>
        /// opens the specified file in the request when provided the correct content type
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="ContentType"></param>
        /// <param name="SaveAsFileName"></param>
        /// <param name="response"></param>
        public static void ResponseOpenFile(string FileName, string ContentType, string SaveAsFileName, HttpResponse response)
        {
            response.Clear();
            response.AddHeader("content-disposition", "attachment;filename=" + SaveAsFileName);
            response.ContentType = ContentType;
            response.Cache.SetCacheability(HttpCacheability.NoCache);
            response.WriteFile(FileName);
            response.End();
        }
 
        /// <summary>
        /// opens the specified text as a file in the request when provided the correct content type
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="ContentType"></param>
        /// <param name="SaveAsFileName"></param>
        /// <param name="response"></param>
        public static void ResponseOpenFileText(string FileText, string ContentType, string SaveAsFileName, HttpResponse response)
        {
            response.Clear();
            response.AddHeader("content-disposition", "attachment;filename=" + SaveAsFileName);
            response.ContentType = ContentType;
            response.Cache.SetCacheability(HttpCacheability.NoCache);
            response.Write(FileText);
            response.End();
        }
}

Open in new window

Avatar of madhevan_pillai
madhevan_pillai
Flag of India image

Hi try this

    /// <summary>
        /// opens the specified text as a file in the request when provided the correct content type
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="ContentType"></param>
        /// <param name="SaveAsFileName"></param>
        /// <param name="response"></param>
        public static void ResponseOpenFileText(string FileText, string ContentType, string SaveAsFileName, HttpResponse response)
        {
            response.Clear();
            response.AddHeader("content-disposition", "attachment;filename=" + SaveAsFileName);
            response.ContentType = ContentType;
            //response.Cache.SetCacheability(HttpCacheability.NoCache);
            response.Write(FileText);
            response.End();
        }
ASKER CERTIFIED SOLUTION
Avatar of madhevan_pillai
madhevan_pillai
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of CompuFit
CompuFit

ASKER

u fixed this problem
IE => download to disk

But i still don't have an extension with Firefox
Does SaveAsFileName has extension?. i tested in my machine it is working .
When i debug
SaveAsFileName  => fileInfo.Name => "18010001-08403490445030709 test nv.o2l" (string)

Maybe because it is an unknown extension?
I have attached a screen shot when downloading the file with Firefox
Naamloos.jpg
Hi
i cld see the ext
Test.bmp
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The space in the filename seems to break it.