Solved

Flash SWF in iFrame using System.IO.FileStream in ASP.NET problem

Posted on 2009-07-14
8
1,317 Views
Last Modified: 2012-05-07
I can show a PDF in an iFrame by passing a URL parameter to the iFrame content page. I thought if I pass in the swf file name and change
   Me.Page.Response.ContentType = "application/pdf"
to
   Me.Page.Response.ContentType = "application/swf"
then the flash file would show in the iFrame but its just blank. The flash file will show if I hardwire the reference and open the iframe content file directly.
Is there a better was to do this, say, with JS ?

The page containing the iFrame has this:
<div><iframe src="flashframe_blank.aspx" width="100%" height="600" id="FlashFrame01" name="FlashFrame01" marginwidth="10" frameborder=0 marginheight="10"><p>Your browser does not support iframes.</p>
 </iframe></div>
Dim dataFilePath As String = ""

 dataFilePath = Me.Page.Server.MapPath("..\App_Data\uploadedfiles\") + CaseName + "\" + FirmName + "\"

 Dim fileExists As Boolean = System.IO.File.Exists(dataFilePath + fileName)

            If fileExists Then

                ' Set up file stream object.

                Dim fs As System.IO.FileStream

                fs = New System.IO.FileStream( _

                          dataFilePath + fileName, _

                          System.IO.FileMode.Open, _

                          System.IO.FileAccess.Read, _

                          System.IO.FileShare.Read)			

                Try

                    ' Read the contents of the file.

                    Dim bufSize As Integer = CType(fs.Length, Integer)

                    Dim buf(bufSize) As Byte

                    Dim bytesRead As Integer = fs.Read(buf, 0, bufSize)
 

                    ' Set up parameters for file download.

                    Me.Page.Response.ContentType = "application/pdf"	

                    Me.Page.Response.AppendHeader("Content-Disposition", "inline; filename=" + Chr(34) + fileName + Chr(34))

                    Me.Page.Response.OutputStream.Write(buf, 0, bytesRead)

                    Me.Page.Response.End()

                Catch ex As Exception

                    Utils.MiscUtils.RegisterJScriptAlert(Me, "UNIQUE_SCRIPTKEY", ex.Message)

                Finally				

                    fs.Flush()

                    fs.Close()

                End Try

            Else

                Utils.MiscUtils.RegisterJScriptAlert(Me, "FILE_NOT_FOUND", "File cannot be found to download")

            End If

Open in new window

0
Comment
Question by:bcombe
  • 4
  • 4
8 Comments
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24856135
Have you considered trying to open an HTML document in your IFRAME instead?  Generally Flash generates an HTML wrapper when it publishes an SWF file, and you can adjust your settings to format the SWF how you want it within the HTML.  I've not done what you're trying to do, but this may be an easy solution.  Just a thought!
0
 

Author Comment

by:bcombe
ID: 24858948
I tried this and it works Boboran but I dont know how to get vaiables from the code behind to the aspx page where I would plug it into the javascript ive got to construct the parameter inputs that make up that html wrapper for the swf. do you know?
0
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24859115
Wow, that's a lot of steps.  So your issue isn't the loading of the SWF in the IFRAME, it's the sending of variables to that SWF?  Sorry I missed that.

I've had problems passing variables to an SWF before too.  One potential workaround is to have your code behind write a database entry, then create a simple PHP or ASP page that queries the database and echoes a simple query string... then within your flash file, you can include that PHP or ASP page to get the values.  I've done that before and found it very helpful.  Thus the process of execution would be something like this:
  • visitor loads page
  • code behind executes, writing values to db
  • swf is loaded in iframe
  • swf looks for include file
  • when include file is loaded, it executes and generates query string
  • swf imports query string but sees only simple text, which it parses as parameter values
The only additional step you would need to make something like this work is a unique key so that when your include file executes, it knows which database values to pull.  So, whichever page links to this page (by "this page," I mean the one with the code behind), have it generate a unique key string which it then passes to "this page."  Now both your code behind and your "code in front" can access that same unique string... the code behind can use it to generate the database entry, and the "code in front" can add it as an argument to the SWF file (e.g. <iframe src="test.swf?id=[uniquekey]">), which your ActionScript can then relay as a single querystring argument for the include file.

Does that make sense?  Rather top level, I know, but hopefully it gives you some ideas.  I've done this very thing in the past and it's saved me a lot of heartache.  Anyway, let me know if you need any clarification!
0
 

Author Comment

by:bcombe
ID: 24861377
thankx, I think I have a visual here - please let me do another round of testing with my dummy pages and come back
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:Bobaran98
ID: 24861641
No prob. :-)
0
 

Author Comment

by:bcombe
ID: 24861659
Quick thought/question,

I have page that has the table where I have the field that has the filename.

I have the field setup as a link where the onclick successfully sends the filename as a URL parameter to the displaying container page called "FlashContainer.aspx".

The FlashContainer page has an iFrame on it. The source of the iFrame is another page called "FlashFrameContent.aspx"

Heres where I'm confused - I can see that I could put the swf file name into a session variable and access that in the FlashFrameContent.aspx.vb codebehind whereupon I would make use it in the aspx page like '<%=local_SWF_FileName%>' to construct the input parameter values for the JS for the swf.

What might be better than session? Also is the iFrame absolutely neccessary - is it making the page a whole lot bigger/heavier/slower than a more elegant approach?

I know I'm asking several questions here, sorry
0
 
LVL 8

Accepted Solution

by:
Bobaran98 earned 500 total points
ID: 24861859
I'm not exactly sure what your questions are, but I'll try to explain all of it in a bit more detail.

I think using an ASP session variable would be your best bet.  Let's say you create a new db table called tblSwfVars with an autonumber key column, then one column for each argument you wish to pass into the SWF (or you could just have one TEXT column for the entire formatted querystring--your choice).

Anyway, every time the FlashContainer.aspx page is loaded, your code should check to see whether you have Session("SwfID") defined.  If not, create a new entry in tblSwfVars, get the key ID back, and write it to Session("SwfID").  Anytime you load FlashFrameContent.aspx, have it also check for Session("SwfID"), and if it's not defined, redirect to FlashContainer.aspx (because the only time it should be undefined is if a visitor has bookmarked FlashContent.aspx and visited it directly).

When FlashContent.aspx loads, the URL for your SWF file should automatically include a querystring with one parameter: "?id="&Session("SwfID").  Then in the code behind, after it generates the arguments, it writes those arguments to the tblSwfVars table using the Session("SwfID").

As far as the IFRAME goes, it's probably not necessary, and it may indeed be making things more complex than they need to be.  If you generate an HTML file with the SWF when you publish your flash, you should be able to just copy/paste the embed and object tags into FlashContent.aspx.

Finally, you may or may not know this, but you don't actually need to use any JavaScript to display your SWF-- you have two options, really, using JavaScript and using straight HTML.  I think by default, that HTML file is generated with both, and the HTML code is only active if JavaScript is disabled.  I believe there are some drawbacks to going the non-JS route, but I couldn't tell you off the top of my head what they are, and depending on what you're doing in your flash, it may not matter.

Is this helpful?  Sorry for writing so much... what can I say?  I'm an aspiring novelist! :-)

0
 

Author Closing Comment

by:bcombe
ID: 31603518
Thank you for your help - it led me in the right direction!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

746 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

12 Experts available now in Live!

Get 1:1 Help Now