• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

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?
0
mpp1031
Asked:
mpp1031
  • 3
  • 2
1 Solution
 
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 opened.....so, you can ask user where he wants to save file and then write into that....instead of creating temporary file in server.
0
 
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?
0
 
Maulik ModiCommented:
Absolutely, You got me perfect!!!

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

I suggest you to look at this article :
http://support.microsoft.com/kb/319180/

Hope it helps!!!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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( "," )
                        EndFor
                        sbHeader.Replace( ",", "", sbHeader.Length-1, 1 )
                        sbHeader.Append( MY_NEWLINE )
                  EndIf
                  
                  ForEach Name( objColumn ) Collection( objTable.Columns )
                        sb.Append( "$$QUOTES$$" + objRow(objColumn).ToString() + "$$QUOTES$$" )
                        sb.Append( "," )
                  EndFor
                  sb.Replace( ",", "", sb.Length-1, 1 )
                  sb.Append( MY_NEWLINE )
                  EndFor
            EndFor
            
            //      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() )
        sw.Close()

Response.Redirect( CSV_FILENAME )
EndSr

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

Thanks for all the help...



0
 
mpp1031Author Commented:
Um, Mr. Moderator... I didn't *really* use this Expert's solution, can I give partial points?
0
 
PAQ_ManCommented:
PAQed with points refunded (250)

PAQ_Man
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now