How to play SWF file in ASP.NET

No.of Views2551
Bookmarked0 times
Downloads 
Votes0
By  ansari.najmul@gmail.com   On  16 Feb 2010 03:02:27
Tag : ASP.NET , How to
This article demostrates how to play an swf (flash) file in asp.net.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

This article demonstrates how to play an swf file (flash) in asp.net. There is some additional requirement that browser must have plug-in installed in order make this work.In this project you need to upload a swf file and then click on the uplaod file to run the uploaded file.

Using the code


It is very simple to create and to paly swf file.

Following are the steps to create and play swf file.

* Step 1. Open Microsoft Visual Studio. Create a web site and named it PlaySwfFile.
* Step 2. Create an .aspx file and named it PlaySwfFile.aspx.
* Step 3. Design the form that looks like this.

Image Loading


* Step 4. Or copy and paste the code in PlaySwfFile.aspx file inside the tag

<form id="form1" runat="server">
        <table width="410px">
            <tr>
                <td align="center"  valign="top">
                    <table border="0" cellpadding="2" cellspacing="3" style="width: 400px;">
                        <tr>
                            <td valign="top" style="width: 150px">
                            </td>
                            <td align="left" valign="top" style="width: 200px">
                                <asp:Label ID="lblMsg" CssClass="tdMessage"  Text ="" runat="server"></asp:Label></td>
                        </tr>
                        <tr>
                            <td valign="top" class="tdText" align="left">
                              <nobr> Select a file</nobr> </td>
                            <td valign="top" style="text-align: left">
                                <asp:FileUpload ID="fUpload" runat="server" Width="300px" /></td>
                        </tr>
                        <tr>
                            <td align="left" valign="top">
                                <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /></td>
                            <td align="left" valign="top">
                                <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"
                                    width="200" height="100">
                                    <param name="movie" value="<% =swfFileName%>" />
                                    <param name="quality" value="high" />
                                    <embed src="<% =swfFileName%>" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer"
                                        type="application/x-shockwave-flash" width="200" height="100"></embed>
                                </object>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </form>

 

Now Add a namespace System.IO; to the PlaySwfFile.aspx.cs file ,Declare a variable above page_load() method .Write code in class file.

 

public string swfFileName = "";

// Now double click on the upload button and ddd the following code below

protected void btnUpload_Click(object sender, EventArgs e)
{
if (fUpload.FileContent.Length >0 && IsVaildFile())
{
string Path = GetUplaodImagePhysicalPath();
DirectoryInfo dirUploadImage = new DirectoryInfo(Path);
if (dirUploadImage.Exists == false)
{
dirUploadImage.Create();
}
string fileUrl = Path + fUpload.PostedFile.FileName;
fUpload.PostedFile.SaveAs(fileUrl);
swfFileName = "image/" + fUpload.PostedFile.FileName;
}
}
private bool IsVaildFile()
{
string swfExt = System.IO.Path.GetExtension(fUpload.PostedFile.FileName);
switch (swfExt)
{
case ".swf":
return true;
default:
{
lblMsg.Text = "Please select only swf file.";
return false;
}
}
}

string GetUplaodImagePhysicalPath()
{
return System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "image\\";
}

}

 

Now run the project and upoad a swf file and click on upload button. you will get the result.

Points to Remember


In the .cs page you will get a public variable named swfFileName. This is the variable which is used in the aspx page to assign as the source of . Here serves as swf player.
In aspx file the public variable swfFileName is accessed as.

Sample Project Source

Download source files -2 kb

 
Sign Up to vote for this article
 
About Author
 
ansari.najmul@gmail.com
Occupation-Software Engineer
Company-NetEdge Computing Solutions
Member Type-Fresh
Location-India
Joined date-12 Aug 2009
Home Page-dotnetlogix.com
Blog Page-
 
 
Other popularSectionarticles
Comments
By:bhaga singhDate Of Posted:5/17/2011 2:28:40 AM
Not work
hi this good is good and work on a page..but when i use this code in my website and give a swf file url dynamicley than its not work..its show only image not play swf file ..so pls help
By:JasonDate Of Posted:1/11/2011 11:48:26 AM
Mr
Hi there This works well except that the browser remains in a downloading state even though all controls of the page have been downloaded. I am guessing this is because the swf control is waiting to have it's src/value (of swfFileName) set. Is there any way to get the browser to be in a downloaded (done) state before uploading the swf file?
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top