Solved

Save Flash file (.swf ) to database as binary file

Posted on 2009-05-14
10
2,558 Views
Last Modified: 2012-05-07
How to Save Flash file (.swf ) to database as binary file and read it again at run time application ??
0
Comment
Question by:Rewaya
  • 5
  • 5
10 Comments
 
LVL 39

Expert Comment

by:abel
Comment Utility
It is actually quite straightforward. An SWF is a binary file like any other (picture, movie) and can be saves as a BLOB to SQL Server. An excellent example of how to do so yourself is provided by CodeProject: http://www.codeproject.com/KB/database/UploadPicturesSQLServer.aspx, which includes all source code (not that much, like I said, quite straightforward).
0
 

Author Comment

by:Rewaya
Comment Utility

OK nice point is
In Image i can say
Image im = Image.FromStream(ms);
But there is nothing like this in Flash object load (.swf) file as stream (Byte array);
So How to solve this.
LoadMovie function of flash take url of the file.

 
 
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Think you were looking at the wrong spot, where the image is requested. Inside UploadFile is the parts that you need. Basically, this part (and picture would contain your flash, the FileName would be the name to your flash file on disk):



#region Reading file
 

    fs = new FileStream(FileName,FileMode.Open);
 

    // Finding out the size of the file to be uploaded

    FileInfo fi = new FileInfo(FileName);

    long temp = fi.Length;

    int lung = Convert.ToInt32(temp);

 

    // Reading the content of the file into an array of bytes.

    byte[] picture=new byte[lung];

    fs.Read(picture,0,lung);

    fs.Close();
 

#endregion
 

// put file into database, check the function by the same name on that page:

long result = uploadFileToDatabase(picture,fi.Name);

Open in new window

0
 

Author Comment

by:Rewaya
Comment Utility
sorry i acn't understand what you mean by looking at the wrong spot .
i have flash control in the application
to load Movie i call LoadMovie function which take URl (string of (.swf ) file on the disk)
 
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
> sorry i acn't understand what you mean by looking at the wrong spot .

you posted a snippet about "Image im = Image.FromStream(ms); " which I tried to explain was not relevant to your situation. It is under that link, yes, but has nothing to do with saving a file to the database.

Your original question was:

"Save Flash file (.swf ) to database as binary file"
maybe I misunderstand and there's no database involved at all? The code on that link and above in my previous post shows how to do exactly that: save any file to the database.

Can you explain me what you want if it is not this? Are you using ASP.NET (you posted in C#, so I assumed that) or something else?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Rewaya
Comment Utility
Simply I want to read (.swf) file after converted to binary and insert it in Database .
i want to read .swf File as byte array and display it .
Using C3 desktop application
0
 

Author Comment

by:Rewaya
Comment Utility
Using C# Desktop application
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
Comment Utility
> Simply I want to read (.swf) file after converted to binary and insert it in Database .
good, that's what the link shows you and the code snippet above shows how to get the byte array

> i want to read .swf File as byte array and display it .
good, that's also what the links shows you. Store it locally and then load it to your app. Then tell me, what parts are unclear?

But somehow you don't like the code on that page, it seems. I don't understand why, but  here's another, perhaps easier one, that explains how to store files from disk in a BLOB and how to retrieve them and save them back to disk: http://www.akadia.com/services/dotnet_read_write_blob.html

-- Abel --
0
 

Author Comment

by:Rewaya
Comment Utility

Nice, This code for reading file and insertion in the Database
And the Second for reading from Database
File was create but with double size , if the original file size = 80 the output file after reading from database =159 if it 43 the output =86
So file was wrong and can't open.

#region Reading & Insertion

            SqlConnection _conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ToString()); ;

            SqlCommand Comm = new SqlCommand("nsert into [Table_1](ID,Expl) values(@ID,@Expl) ", _conn);

            ////writing

            FileStream fs = new FileStream("FlashTest1.swf", FileMode.OpenOrCreate, FileAccess.Read);
 

            StreamReader sr = new System.IO.StreamReader(fs, System.Text.Encoding.Default);
 

            string line = sr.ReadToEnd();

            byte[] Script_byte = (new UnicodeEncoding()).GetBytes(line);
 

            try

            {

                Comm.AddParameter("ID", 1, SqlDbType.Int, ParameterDirection.Input);

                Comm.AddParameter("Expl", Script_byte, SqlDbType.VarBinary, ParameterDirection.Input);

_conn.Open();

               Comm.ExecuteNonQuery();

               _conn.Close();           

            }

           catch (Exception ex) { }

            #endregion
 
 

=================================

     #region Reading File From DataBase
 

            SqlConnection _conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB "].ToString()); ;

            SqlCommand Comm = new SqlCommand(

                "SELECT [Expl]  FROM [MyDB].[dbo].[Table_1] where ID=@ID ", _conn);
 

            Comm.Parameters.Add("@ID", SqlDbType.Int).Value = 1;
 

            FileStream fs;                          // Writes the BLOB to a file (*.bmp).

            BinaryWriter bw;                        // Streams the BLOB to the FileStream object.

            int bufferSize = 100;                   // Size of the BLOB buffer.

            byte[] outbyte = new byte[bufferSize];  // The BLOB byte[] buffer to be filled by GetBytes.

            long retval;                            // The bytes returned from GetBytes.

            long startIndex = 0;                    // The starting position in the BLOB output.
 

            // Open the connection and read data into the DataReader.

            _conn.Open();

            SqlDataReader myReader = Comm.ExecuteReader(CommandBehavior.SequentialAccess);

            try

            {

                while (myReader.Read())

                {

                    // Get the employee id, which must occur before getting the employee.

                    // emp_id = myReader.GetInt32(0).ToString();
 

                    // Create a file to hold the output.

                    fs = new FileStream("Flash1.swf",

                                        FileMode.OpenOrCreate, FileAccess.Write);

                    bw = new BinaryWriter(fs);
 

                    // Reset the starting byte for the new BLOB.

                    startIndex = 0;
 

                    // Read the bytes into outbyte[] and retain the number of bytes returned.

                    retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
 

                    // Continue reading and writing while there are bytes beyond the size of the buffer.

                    while (retval == bufferSize)

                    {

                        bw.Write(outbyte);

                        bw.Flush();
 

                        // Reposition the start index to the end of the last buffer and fill the buffer.

                        startIndex += bufferSize;

                        retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
 

                    }
 

                    // Write the remaining buffer.

                    bw.Write(outbyte, 0, (int)retval);

                    bw.Flush();
 

                    // Close the output file.

                    bw.Close();

                    fs.Close();

                }

            }

            catch (Exception ex)

            {
 

            }

            // Close the reader and the connection.

            myReader.Close();

            _conn.Close();

            #endregion

Open in new window

0
 
LVL 39

Expert Comment

by:abel
Comment Utility
> Nice, This code for reading file and insertion in the Database > And the Second for reading from Database
yes, indeed.

> File was create but with double size ,

intriguing. But you are using Unicode encoding. Why? You use a binary blob and you try an encoding scheme on it? That was not on the page I showed you. Using an encoding scheme will change the meaning of the bytes and will write it down differently. In this case, you open it with the default encoding (probably ascii or something) and you write it with unicode. Result: double byte characters (utf-16, would be my guess).

remove any encoding (just don't use it) or use encoding Ascii everywhere. Don't mix, and definitely don't mix when using binary data.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now