Solved

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

Posted on 2009-05-14
10
2,588 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
ID: 24383644
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
ID: 24384033

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
ID: 24384079
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Rewaya
ID: 24384135
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
ID: 24384200
> 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
 

Author Comment

by:Rewaya
ID: 24384288
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
ID: 24384291
Using C# Desktop application
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24384448
> 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
ID: 24401688

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
ID: 24401911
> 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

773 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