Click here to Skip to main content
14,661,413 members
Articles » Web Development » ASP.NET » General
Article
Posted 28 Oct 2008

Stats

150.5K views
5.1K downloads
26 bookmarked

How To Play SWF File In Asp.Net

Rate this:
3.82 (27 votes)
Please Sign up or sign in to vote.
3.82 (27 votes)
28 Oct 2008CPOL
This article demostrates how to play an swf (flash) file in asp.net.

Introduction<o:p> 

This article demonstrates how to play an swf file (flash) in asp.net.  <o:p>

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.<o:p>

Using the code<o:p>

It is very simple to create and to paly swf file.<o:p>

Following are the steps to create and play swf file. <o:p>

  • Step 1. Open Microsoft Visual Studio. Create a web site and named it PlaySwfFile.<o:p>
  • Step 2. Create an .aspx file and named it PlaySwfFile.aspx. <o:p>
  • Step 3. Design the form that looks like this. <o:p>
  • swfImage.gif 
  • Step 4. Or copy and paste the code in  PlaySwfFile.aspx file inside the <body> tag <o:p>
   <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  

  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\\";
    }
  
} 

<o:p> 

Now run the project and upoad a swf file and click on upload button. you will get the result.<o:p>

Points to Remember <o:p>

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>. <o:p>

Here <object>  serves as swf player.<o:p>

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

<o:p>  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Najmul Hoda
Software Developer (Senior) Birla Soft
India India
Najmul Hoda is a Master of Computer Application.
He has worked with .Net technologies in web development and has been programming since 2007.
He is very comfortable in various languages,RDBMS,C# to VB.NET with Access & MS SQLServer from Javascript,AJAX to ASP.NET, MVC.


Khata Wata (Free Online Udhar Khata, Bahi Khata)
Download from play store


Comments and Discussions

 
QuestionMovie not loaded message when pointing embed src to "d:\abc\story.swf" Pin
SURESH_KATARI7-Jul-14 20:20
MemberSURESH_KATARI7-Jul-14 20:20 
QuestionHow to convert and implement this code in VB9 Pin
rkear8917-Jan-13 3:04
Memberrkear8917-Jan-13 3:04 
I converted most of the code to vb msyelf and have some success but am running into some errors. Would anyone be able to help? I have pasted my code below.

The first part I have in file PlaySwfFile.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="PlaySwfFile.aspx.vb" Inherits="PlaySwfFile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
   <form id="form1" runat="server">
        <table style="height: 440px; width: 730px">
            <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" 
                                    style="width: 638px; height: 412px">
                                    <param name="movie" value="<% ="Design for Manufacturing - Norplas 16JAN13.swf"%>" />
                                    <param name="quality" value="high" />
                                    <embed src="<% ="Design for Manufacturing - Norplas 16JAN13.swf"%>" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer"
                                        type="application/x-shockwave-flash" width="" height=""></embed>
                                </object>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>


The next portion of code is file PlaySwfFile.aspx.vb

Imports System.IO
Imports System.Web.UI.Page

Partial Class PlaySwfFile
    Inherits System.Web.UI.Page

    Dim validNum As Integer
    Dim swfFileName As String
    Dim GetUplaodImagePhysicalPath As String
    Dim Path As String
    Dim dirUploadImage As String


    Public Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Call IsValidFile()

        GetUplaodImagePhysicalPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath(+"image\\")

        If fUpload.FileContent.Length > 0 & validNum = 1 Then

            Path = GetUplaodImagePhysicalPath
            dirUploadImage = New DirectoryInfo(Path).ToString
            'DirectoryInfo(dirUploadImage = New DirectoryInfo(Path))
            If dirUploadImage Then
                dirUploadImage = New DirectoryInfo(Path).ToString
            End If

            Dim fileUrl As String
            fileUrl = Path + fUpload.PostedFile.FileName
            fUpload.PostedFile.SaveAs(fileUrl)
            swfFileName = "image/" + fUpload.PostedFile.FileName
        End If
    End Sub

    Public Sub IsValidFile()

        Dim swfExt As String
        Dim lblMsg As String
        lblMsg = "Please select swf file."

        swfExt = System.IO.Path.GetExtension("Design for Manufacturing - Norplas 16JAN13.swf")  'fUpload.PostedFile.FileName)

        If Not swfExt = ".swf" Then
            lblMsg = "Please select only swf file. Select another file."
            validNum = 0
        Else
            lblMsg = "File Selection Successful."
            validNum = 1
        End If

    End Sub
End Class


Any help or suggestions is much appreciated! Thanks!!
God Bless!

Questionthanks Pin
Raj kumar Rajbhar6-Nov-12 18:03
MemberRaj kumar Rajbhar6-Nov-12 18:03 
GeneralMy vote of 5 Pin
Renju Vinod19-Aug-12 22:36
professionalRenju Vinod19-Aug-12 22:36 
Questionthnks Pin
abhay09810-Feb-12 19:20
Memberabhay09810-Feb-12 19:20 
BugSwf not playing in ie Pin
D.R.Chakravarthy18-Oct-11 23:59
MemberD.R.Chakravarthy18-Oct-11 23:59 
QuestionHow To Play SWF File In Asp.Net - Error: "The given path's format is not supported." Pin
Wijesinghe4-Aug-11 20:10
MemberWijesinghe4-Aug-11 20:10 
AnswerRe: How To Play SWF File In Asp.Net - Error: "The given path's format is not supported." Pin
mohmed216-Feb-12 21:35
Membermohmed216-Feb-12 21:35 
AnswerRe: How To Play SWF File In Asp.Net - Error: "The given path's format is not supported." Pin
AndrewTMyers31-Jul-12 5:35
MemberAndrewTMyers31-Jul-12 5:35 
GeneralRe: How To Play SWF File In Asp.Net - Error: "The given path's format is not supported." Pin
AndrewTMyers31-Jul-12 5:39
MemberAndrewTMyers31-Jul-12 5:39 
QuestionNotSupported Exception was unhandled by user code Pin
akshaychawla10-May-11 23:28
Memberakshaychawla10-May-11 23:28 
AnswerRe: NotSupported Exception was unhandled by user code Pin
Wijesinghe4-Aug-11 20:07
MemberWijesinghe4-Aug-11 20:07 
Generalquestion Pin
shivshankar pal26-Mar-11 3:04
Membershivshankar pal26-Mar-11 3:04 
Generalnice and simple code Pin
ripalsoni14-Mar-11 9:09
Memberripalsoni14-Mar-11 9:09 
Questiona question Pin
ahatamirad9-Nov-10 20:15
Memberahatamirad9-Nov-10 20:15 
GeneralMy vote of 5 Pin
deepak chaudhari snk14-Oct-10 0:03
Memberdeepak chaudhari snk14-Oct-10 0:03 
GeneralNice answer Pin
walsing30-Jul-10 4:33
Memberwalsing30-Jul-10 4:33 
GeneralRe:How to play SWF file in asp.net Pin
yadav awadhesh24-Mar-09 18:17
Memberyadav awadhesh24-Mar-09 18:17 
Generalnice one again Pin
Vimalsoft(Pty) Ltd13-Feb-09 2:20
professionalVimalsoft(Pty) Ltd13-Feb-09 2:20 
Generalcustom logo Pin
kris4413-Jul-11 4:43
Memberkris4413-Jul-11 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.