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

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?
Who is Participating?
PAQed with points refunded (250)

Community Support Moderator
Maulik ModiCommented:
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.
mpp1031Author Commented:
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?
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Maulik ModiCommented:
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!!!
mpp1031Author Commented:
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...

mpp1031Author Commented:
Um, Mr. Moderator... I didn't *really* use this Expert's solution, can I give partial points?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.