locked
Do anybody knows how to download text file? RRS feed

  • Question

  • User1252935300 posted

    Hi Expert

    Do anybody knows how to download a text file? I have created a text file and using some examples in the forums to connect it but it won't work in my case..

    Regards
    akhp84

    Thursday, June 12, 2008 1:12 AM

Answers

  • User845932231 posted

     

            string filePath = Server.MapPath("~") + "/"  + "mytextfile.txt"; //give the correct path here. I am assuming the file is in your root

            System.IO.FileInfo objFile = new System.IO.FileInfo(filePath );
            Response.Clear();
            Response.AddHeader("Content-Length", objFile.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(objFile.FullName);
            Response.Flush();
            Response.End();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 1:41 AM
  • User1252935300 posted

    Hi Anzer 

    May I know what language are you using.. Is it java? Is there a way of changing the language to asp.net.

    But anyway thanks for your response.

    Regards
    Han Poh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 2:12 AM
  • User845932231 posted

    Its C#.. Do you want it in VB.Net?? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 2:21 AM
  • User845932231 posted

            Dim filePath As String = Server.MapPath("~") + "/" + "mytextfile.txt" ' //give the correct path here. I am assuming the file is in your root

            Dim objFile As System.IO.FileInfo = New System.IO.FileInfo(filePath)
            Response.Clear()
            Response.AddHeader("Content-Length", objFile.Length.ToString())
            Response.AddHeader("Content-Disposition", "attachment;filename=" + filename)
            Response.ContentType = "application/octet-stream"
            Response.WriteFile(objFile.FullName)
            Response.Flush()
            Response.End()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 2:35 AM
  • User1252935300 posted

    Hi Anzer

    Thanks again for your helpful response.

    I have declared the "filename" as a object, collection or string, but it can't work also. Do you know why?

    Regards
    akhp84

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 4:19 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 13, 2008 4:13 AM

All replies

  • User845932231 posted

     

            string filePath = Server.MapPath("~") + "/"  + "mytextfile.txt"; //give the correct path here. I am assuming the file is in your root

            System.IO.FileInfo objFile = new System.IO.FileInfo(filePath );
            Response.Clear();
            Response.AddHeader("Content-Length", objFile.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(objFile.FullName);
            Response.Flush();
            Response.End();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 1:41 AM
  • User1252935300 posted

    Hi Anzer 

    May I know what language are you using.. Is it java? Is there a way of changing the language to asp.net.

    But anyway thanks for your response.

    Regards
    Han Poh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 2:12 AM
  • User845932231 posted

    Its C#.. Do you want it in VB.Net?? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 2:21 AM
  • User845932231 posted

            Dim filePath As String = Server.MapPath("~") + "/" + "mytextfile.txt" ' //give the correct path here. I am assuming the file is in your root

            Dim objFile As System.IO.FileInfo = New System.IO.FileInfo(filePath)
            Response.Clear()
            Response.AddHeader("Content-Length", objFile.Length.ToString())
            Response.AddHeader("Content-Disposition", "attachment;filename=" + filename)
            Response.ContentType = "application/octet-stream"
            Response.WriteFile(objFile.FullName)
            Response.Flush()
            Response.End()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 2:35 AM
  • User1252935300 posted

    Hi Anzer

    Thanks again for your helpful response.

    I have declared the "filename" as a object, collection or string, but it can't work also. Do you know why?

    Regards
    akhp84

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 4:19 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 13, 2008 4:13 AM