C# .NET - what is the code for retreive swf file from database using asp.net c#?

Asked By jaheena jamal on 24-Jun-11 12:28 AM
sir,

I want to retreive swf file from mssql database using asp.net c# language. 
what can i do for that?
Jitendra Faye replied to jaheena jamal on 24-Jun-11 12:40 AM
Files are stored in Database in byte[] array, so first you have to take one byte[] array to store fetched file from database,.

Use this code-

Here i m reading file based on id and saving this file to local disk

SqlConnection cn = new SqlConnection("con string");
        cn.Open();
        string strQuery = "Select * from EmpFiles";
        SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
        DataSet ds = new DataSet();
        da.Fill(ds);

        byte[] b = new byte[0];
        b = (Byte[])(ds.Tables[0].Rows[0][1]);

        MemoryStream ms = new MemoryStream(b);

        System.IO.FileStream fs = new System.IO.FileStream(@"c:\myfile.swf", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
        System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
        bw.Write(b);
        bw.Close();

Use this code and let me know.    

TSN ... replied to jaheena jamal on 24-Jun-11 12:50 AM
hi..

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. 
  • swfImage.gif 
  • Step 4. Or copy and paste the code in  PlaySwfFile.aspx file inside the <body> 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  


 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\\";
}
 }
hope this helps you...
jaheena jamal replied to Jitendra Faye on 24-Jun-11 01:40 AM
 sir ,


how can i dynamically bring from database ?
dipa ahuja replied to jaheena jamal on 24-Jun-11 03:19 AM
Use this code:

private void button1_Click(object sender, EventArgs e)
  {
    SqlConnection conn = new SqlConnection("ConnectionString");
    SqlDataAdapter da = new SqlDataAdapter("Select swfFile from table1", conn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    conn.Open();
    //display 1st file
    if (dt.Rows.Count > 0)
    {
      byte[] storedImage = (byte[])dt.Rows[0]["swfFile"];
      Image newImage;       
      Response.BinaryWrite(storedImage);
    }
    
  }
Jitendra Faye replied to jaheena jamal on 24-Jun-11 05:44 AM
The code which i have given you, it is only for dynamic .

Just pass Emp id only.



SqlConnection cn = new SqlConnection("con string");
      cn.Open();
      string strQuery = "Select * from EmpFiles where empid='" + txtEmpID.Text + "' ";
      SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
      DataSet ds = new DataSet();
      da.Fill(ds);

      byte[] b = new byte[0];
      b = (Byte[])(ds.Tables[0].Rows[0][1]);

      MemoryStream ms = new MemoryStream(b);

      System.IO.FileStream fs = new System.IO.FileStream(@"c:\myfile.swf", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
      System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
      bw.Write(b);
      bw.Close();


Try this and let me know.
Kirtan Patel replied to jaheena jamal on 24-Jun-11 07:21 AM
here is neat code to do it 

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("select swf  from UserData where id=1",con);
byte[] data = (byte[])comm.ExecuteScalar();
 
Response.ContentType = "application/x-shockwave-flash";  
/* Display to User on Page */
Response.OutputStream.Write(data, 0, data.Length);

Thanks