Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1402
  • Last Modified:

Calculate SWF Duration using ASP.NET C# CLASS

I have some code adapted from the Codeproject article "Reader of SWF Header File in Pure C#" - Windows Forms Sample.

I have adapted it to a .NET4 web application but it isnt seeing the variables in the FlashHeaderReader CLASS. swfreader.zip

 FlashHeaderReader-Does Not Contain Definition
I am only showing it in a listbox but actually need the variables available for direct insertion into a SQL 2008 Database.

When I had this as a VS Project it didnt show an error.
In all cases the test browser screen immediately displays an error on upload.

When I changed it to a Web App the definition and extension method error displayed.

It is important that the solution to this question does NOT involve 3rd party controls.
Bonus points for integration of Upload Code to SQl2008 casting the different data types.




0
XGIS
Asked:
XGIS
  • 3
  • 2
1 Solution
 
XGISAuthor Commented:
The Code;  Why would the CLASS variables be 'invisible' to the form.
using System;
using System.IO;

namespace SwfHeaderReader
{
    /// <summary>
    /// Summary description for FlashHeaderReader.
    /// </summary>
    public class FlashHeaderReader
    {
        internal int FrameRate
        {
            get { return mFrameRate; }
        }
        internal int FrameCount
        {
            get { return mFrameCount; }
        }

    internal int Width
    {
      get { return mWidth; }
    }
   
    internal int Height
    {
      get { return mHeight; }
    }

    internal bool Error
    {
      get {return mbErrorSignature;}     
    }

        private int mFrameRate=0;
        private int mFrameCount=0;
        private int mVersion=0;
        private bool mIsCompressed=false;
    private int mWidth=0;
    private int mHeight=0;
    private bool mbErrorSignature;

    // To Parse Header
    private byte[] data;
    private int currentIndex;
   

        public FlashHeaderReader(String filename)
        {
      currentIndex=0;
      mbErrorSignature=false;
            FileStream fs = File.OpenRead(filename);
            data = new byte[fs.Length];           
            fs.Read (data, 0, data.Length);
            fs.Close();       
      ParseHeader();           
        }

    /*
     *  Parse the header of the swf format
     *  Format http://www.half-serious.com/swf/format/index.html#header
     *  convert from php : http://www.zend.com/codex.php?id=1382&single=1   
     */
    private void ParseHeader()
    {     
      ReadSignature();     
      mVersion=GetNextByte();
      ReadFileLength();
      ReadFrameSize();                   
      ReadFrameRate();
      ReadFrameCount();
    }

    /*
     * Read first caractere FWS or CWS
     * File compressed are not supported
     */
    private void ReadSignature()
    {
      Byte readingByte=GetNextByte();
      if (readingByte=='C')
      {
        mIsCompressed=true;
        // Not supported
      }
      if (readingByte!='F' && readingByte!='C')
      {
        mbErrorSignature=true;
      }
     
      if (GetNextByte()!='W') mbErrorSignature=true;
      if (GetNextByte()!='S') mbErrorSignature=true;
    }
    /*
     *  Read the RECT Structure : Frame size in twips   
     *  This retrieve from byte the size.
     *  the source php is difficult to translate I have made
     *  something working...
     */
    private void ReadFrameSize()
    {
      int cByte = GetNextByte();
      int NbBits=cByte>>3;
           
      cByte&=7;
      cByte<<=5;

      int currentBit=2;

      int currentValue;

      // Must get all 4 values in the RECT
      for (int numField=0;numField<4;numField++)
      {
        currentValue=0;
        int bitcount = 0 ;
        while (bitcount<NbBits)
        {
          if ((cByte&128)==128)
          {
            currentValue =currentValue + (1<<(NbBits-bitcount-1)) ;
          }          
          cByte<<=1 ;
          cByte &= 255 ;
          currentBit-- ;
          bitcount++ ;
          // We will be needing a new byte if we run out of bits
          if (currentBit<0)
          {
            cByte    = GetNextByte() ;
            currentBit = 7 ;
          }
        }       
       
        // TWIPS to PIXELS
      currentValue/=20;
      switch (numField)
      {
        case 0:
        mWidth = currentValue ;
        break ;
        case 1:
        mWidth = currentValue - mWidth ;
        break ;
        case 2:
        mHeight = currentValue ;
        break ;
        case 3:
        mHeight = currentValue - mHeight ;
        break;
       }
      }       
    }

    /*
     * Read Frame delay in 8.8 fixed number of frames per second  
     */
    private void ReadFrameRate()
    {
      // Frame rate
      byte  fps_decimal,fps_int ;
      fps_decimal = GetNextByte();       
      fps_int     =GetNextByte();
      mFrameRate=fps_int+(fps_decimal)/100;
    }

    private void ReadFrameCount()
    {
      for (int i=0;i<2;i++)
      {
        mFrameCount+= (GetNextByte()<<(8*i)) ;
      }
    }
    /*
     *  Read  FileLength : Length of entire file in bytes   
     *  Not implemented
     */
    private void ReadFileLength()
    {
      //Read Size
      GetNextByte();
      GetNextByte();
      GetNextByte();
      GetNextByte();
    }

    /*
     *  Retrieve one caractere of the buffer.
     */
    private byte GetNextByte()
    {
      Byte result;
      result=data[currentIndex];
      currentIndex++;
      return(result);
    }
    }
}

Open in new window

0
 
raja_ind82Commented:
Why is the property internal in the first place? If you want to have public access to it, make it public. Internal members are accessible only within files in the same assembly

It's possible to use the InternalsVisibleTo Attribute to make a specific assembly able to reach internal properties but from my point of view that's a poor design.

http://msdn.microsoft.com/en-us/library/7c5ka91b(v=vs.80).aspx
0
 
XGISAuthor Commented:
Thankyou Raja. That has helped me to get it closer to functional.
I have modified the code a bit but it now does everything but the CLASS does not apppear to be calculating the values. All values return 0 as shown in the image.

 Invalid SWFHeader Reader
The ASPX - This one using Telerik AsyncUpload.

 
<div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadProgressManager ID="Radprogressmanager1" runat="server" />
        <div style="position: relative;">
            <table>
                <tr>
                    <td id="controlContainer">
                        <telerik:RadUpload ID="RadUpload1" runat="server" InitialFileInputsCount="1" MaxFileInputsCount="1"
                            AllowedFileExtensions=".swf,.m4v" />
                        <telerik:RadProgressArea ID="progressArea1" runat="server" />
                        <asp:Button ID="buttonSubmit" runat="server" CssClass="RadUploadSubmit" OnClick="buttonSubmit_Click"
                            Text="Submit" Style="margin-top: 6px" />
                    </td>
                    <td>
                        <div class="smallModule">
                            <div class="rc1">
                                <div class="rc2">
                                    <div class="rc3" style="width: 240px">
                                        <div class="title" style="color: green">
                                            Uploaded valid files (*.swf, *.m4v):</div>
                                        <asp:Label ID="labelNoResults" runat="server" Visible="True">No uploaded files</asp:Label>
                                        <asp:ListBox runat="server" ID="ListBox1" Width="250" Height="300"></asp:ListBox>
                                        <asp:Repeater ID="repeaterValidResults" runat="server" Visible="False">
                                            <ItemTemplate>
                                                <%#DataBinder.Eval(Container.DataItem, "FileName")%>
                                                (<%#DataBinder.Eval(Container.DataItem, "ContentLength").ToString() + " bytes"%>)<br />
                                                <br />
                                                Mime-type:
                                                <%#DataBinder.Eval(Container.DataItem, "ContentType").ToString()%><br />
                                            </ItemTemplate>
                                        </asp:Repeater>
                                        <div class="title" style="color: red; padding-top: 40px;">
                                            Invalid files:</div>
                                        <asp:Label ID="labelNoInvalidResults" runat="server" Visible="True">No invalid files</asp:Label>
                                        <asp:Repeater ID="repeaterInvalidResults" runat="server" Visible="False">
                                            <ItemTemplate>
                                                File:
                                                <%#DataBinder.Eval(Container.DataItem, "FileName") %>
                                                (<%#DataBinder.Eval(Container.DataItem, "ContentLength").ToString() + " bytes"%>)
                                                <br />
                                                Mime-type:
                                                <%#DataBinder.Eval(Container.DataItem, "ContentType").ToString()%>
                                            </ItemTemplate>
                                        </asp:Repeater>
                                    </div>
                                </div>
                                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>

Open in new window


The C# maybe the SwfHeaderReader code is in the wrong position to receive data.

Essentially two separate processes are occurring here, with neither sharing variables.

 
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using SwfHeaderReader;

public partial class ManipulateUploadedFiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void buttonSubmit_Click(object sender, System.EventArgs e)
    {
        BindValidResults();
        BindInvalidResults();
        DeleteTempFiles();
    }
    private void BindValidResults()
    {
        if (RadUpload1.UploadedFiles.Count > 0)
        {
            foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
            {
                string targetFolder = Server.MapPath("~/App_Upload/Real");
                validFile.SaveAs(Path.Combine(targetFolder, validFile.GetName()), true);

                //SwfHeaderReader Class
                FlashHeaderReader swfreader = new FlashHeaderReader(RadUpload1.UploadedFiles);

                //SwfHeaderReader ListBox
                ListBox1.Items.Clear();
                ListBox1.Items.Add("Frames : " + swfreader.FrameCount);
                ListBox1.Items.Add("Width : " + swfreader.Width);
                ListBox1.Items.Add("Height : " + swfreader.Height);
                ListBox1.Items.Add("Rate : " + swfreader.FrameRate);
                ListBox1.Items.Add("Count : " + swfreader.FrameCount);

                //temp Check
                string swftxfer = swfreader.FrameCount.ToString();
                Label1.Text = swftxfer.ToString();

            }
            labelNoResults.Visible = false;
            repeaterValidResults.Visible = true;
            repeaterValidResults.DataSource = RadUpload1.UploadedFiles;
            repeaterValidResults.DataBind();
        }
        else
        {
            labelNoResults.Visible = true;
            repeaterValidResults.Visible = false;
        }
    }

    private void BindInvalidResults()
    {
        if (RadUpload1.InvalidFiles.Count > 0)
        {
            labelNoInvalidResults.Visible = false;
            repeaterInvalidResults.Visible = true;
            repeaterInvalidResults.DataSource = RadUpload1.InvalidFiles;
            repeaterInvalidResults.DataBind();
        }
        else
        {
            labelNoInvalidResults.Visible = true;
            repeaterInvalidResults.Visible = false;
        }
    }

    /// <summary>
    /// For the purpose of this demo we delete the files collection in the targetfolder
    /// to prevent it from growing infinitely 
    /// </summary>
    private void DeleteTempFiles()
    {
        string targetFolder = Server.MapPath("~/App_Upload/Temp");

        DirectoryInfo targetDir = new DirectoryInfo(targetFolder);

        try
        {
            foreach (FileInfo file in targetDir.GetFiles())
            {
                if ((file.Attributes & FileAttributes.ReadOnly) == 0) file.Delete();
            }
        }
        catch (IOException)
        {
        }
    }
}

Open in new window

0
 
XGISAuthor Commented:
not sure why i even use this site
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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