• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2758
  • Last Modified:

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

How to Save Flash file (.swf ) to database as binary file and read it again at run time application ??
0
Rewaya
Asked:
Rewaya
  • 5
  • 5
1 Solution
 
abelCommented:
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
 
RewayaAuthor Commented:

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
 
abelCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RewayaAuthor Commented:
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
 
abelCommented:
> 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
 
RewayaAuthor Commented:
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
 
RewayaAuthor Commented:
Using C# Desktop application
0
 
abelCommented:
> 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
 
RewayaAuthor Commented:

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
 
abelCommented:
> 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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