Creating an <A HREF> that streams data to browser on-the-fly

Posted on 2005-04-08
Last Modified: 2010-04-07
I want to create a link on a ASP.NET page that grabs data from a datasource and then outputs that stream to the browser as a text file.

For example, I have data that the user will probably want to see in Excel.  Rather than write the data to the web server disk, is there a way that I can work it so that this is generated in the resultant HTML.:

<a href="textfile.csv">Excel Formatted Data</a>

When the user clicks the link, because it's a CSV file, it will open in Excel (usually).  I want the "textfile.csv" to be the most recent snapshot of my datasource, BUT I don't want to have to (or probably won't be able to) write a temporary file to the web server disk.

Is there a "virtual file" or something that can be created in a memory or something?
Question by:mpp1031
    LVL 6

    Expert Comment

    Let me answer part of your Q:

    Q: Is there a "virtual file" or something that can be created in a memory or something?
    A: Yes it can be created, but I think only the things that physically reside on disk can be, you can ask user where he wants to save file and then write into that....instead of creating temporary file in server.

    Author Comment

    Can you give me a brief example?  I'm not sure I'm understanding.

    You're saying that I can have "some data" temporarily in memory space on the server, provide a link/button/whatever for the user;  User clicks link, and then is prompted as to where they would like to save "the file"?

    If that's the case, then that's probably perfect.  How do I do it?
    LVL 6

    Expert Comment

    Absolutely, You got me perfect!!!

    I posted answer based on my view of possible soultion, its not out there with,cosidering this problem we both are at the same level.

    I suggest you to look at this article :

    Hope it helps!!!

    Author Comment

    I ended up not using the KB article (a method to write data as Excel XML).  It was a very elegant and sophisticated solution, but I didn't have the time to adapt it to my application.  Thanks, though...

    What I did end up doing was using the Streamwriter object to write a textfile to the server, and then linked to that same document.

    It looks like thus:

    BegSr btnLinkTest_Click Access(*Private)  Event(*this.btnLinkTest.Click)
          DclSrParm sender      Type(*Object)
          DclSrParm e                  Type(System.EventArgs)
          DclFld      objData            Type( System.Data.DataSet )
          DclFld      objTable      Type( System.Data.DataTable )
          DclFld  objRow            Type( System.Data.DataRow )
          DclFld      objColumn      Type( System.Data.DataColumn )
          DclFld  sb                  Type( System.Text.StringBuilder ) New()
          DclFld  sbHeader      Type( System.Text.StringBuilder ) New()
          PASSYEAR                  = Convert.ToInt32( ViewState( "PREVYEAR" ))
          PASSNMBR                  = 0

          objData = ListIO.GetListForExport( PASSYEAR, PASSNMBR )
          ForEach Name( objTable ) Collection( objData.Tables )
                ForEach Name( objRow ) Collection( objTable.Rows )
                      If ( sbHeader.Length = 0 )
                            ForEach Name( objColumn ) Collection( objTable.Columns )
                                  sbHeader.Append( "$$QUOTES$$" + objColumn.ColumnName.ToString() + "$$QUOTES$$" )
                                  sbHeader.Append( "," )
                            sbHeader.Replace( ",", "", sbHeader.Length-1, 1 )
                            sbHeader.Append( MY_NEWLINE )
                      ForEach Name( objColumn ) Collection( objTable.Columns )
                            sb.Append( "$$QUOTES$$" + objRow(objColumn).ToString() + "$$QUOTES$$" )
                            sb.Append( "," )
                      sb.Replace( ",", "", sb.Length-1, 1 )
                      sb.Append( MY_NEWLINE )
                //      Replace $$QUOTES$$ with actual character
                sb.Replace( "$$QUOTES$$", MY_QUOTE )
                sbHeader.Replace( "$$QUOTES$$", MY_QUOTE )
                //  Append data to header stringbuilder object
                sbheader.Append( sb.ToString() )
                // Create an instance of StreamWriter to write text to a file.
            DclFld timestamp      Type( *String )
            DclFld sw            Type( StreamWriter )
            //      Get LocalPath of application directory on web server
            DclFld strLocalPath Type ( *String )
            strLocalPath = Page.Request.PhysicalApplicationPath.ToString()
            // Add some text to the file.
            sw = *New StreamWriter ( strLocalPath + CSV_FILENAME )
            sw.Write( sbHeader.ToString() )

    Response.Redirect( CSV_FILENAME )

    It's kinda fumbly and dirty, but it does what I need it to do.

    Thanks for all the help...


    Author Comment

    Um, Mr. Moderator... I didn't *really* use this Expert's solution, can I give partial points?

    Accepted Solution

    PAQed with points refunded (250)

    Community Support Moderator

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now