User Id :    Password :      New Member   Forgot Password  
 
How To Play SWF File In Asp.Net
Description This article demostrates how to play an swf (flash) file in asp.net.   No. of Views     6107
  Rating     5
Author Najmul Hoda   Posted On     22 Aug 2010
Tags ASP.NET,Java Script,HTML    

Sample Code   Download Code

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.

 

Loading image....

 

  • Step 4. Or copy and paste the code in  PlaySwfFile.aspx file inside the <body> tag
<table style="width: 400px;" border="0" cellpadding="2" cellspacing="3">
        <tr>
            <td style="width: 150px;" valign="top">
            </td>
            <td style="width: 200px;" valign="top" align="left">
                <asp:Label ID="lblMsg" CssClass="tdMessage" Text="" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="tdText" valign="top" align="left">
                <nobr> Select a file</nobr>
            </td>
            <td style="text-align: left;" valign="top">
                <asp:FileUpload ID="fUpload" runat="server" Width="300px"></asp:FileUpload>
            </td>
        </tr>
        <tr>
            <td valign="top" align="left">
                <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click">
                </asp:Button>
            </td>
            <td valign="top" align="left">
                <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="%3C%%20=swfFileName%%3E" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer"
                        type="application/x-shockwave-flash" width="200" height="100">
                </object>
            </td>
        </tr>
    </table>

//  Now Add a namespace System.IO; to the  PlaySwfFile.aspx.cs file 

// Declare a variable above page_load() method  

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 <object>

Here <object>  serves as swf player.

In aspx file the public variable swfFileName is accessed as <% =swfFileName%>

 

 

 

About Author

About Author He is a team lead, having 8+ years of experience. He loves writing technical article for .Net developers. Najmul Hoda
No Photo
 
Country United States
Company BirlaSoft
Home Page http://www.magixchat.com

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Annonymous on 02 Sep 2011 at 10:56 AM
 
I am toatlly wowed and prepared to take the next step now.
 
 
 
Posted By Gaurav on 23 Aug 2010 at 12:21 AM
 
Nice One ...keep it up Thanks
 
 
   
Write your comment here.
Comment
Verification Code