在ASP.NET中加载SWF文件

时间:2011-02-12 19:09:21

标签: asp.net sql-server flash

我坚持在我的aspx上加载swf。我应该从数据库中检索swf并在页面上显示。我的代码设法在从db检索后在本地文件夹上创建swf ...但我似乎无法在下载后自动在aspx上显示它...而aspx中的代码应该是什么?...任何想法的人?... thx ......

 public string swfFileName = "";


protected void Page_Load(object sender, EventArgs e)
{
    string swfToDbConnStr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    try
    {

        using (SqlConnection connection = new SqlConnection(swfToDbConnStr))
        {
            // Assumes that connection is a valid SqlConnection object.
            SqlCommand command = new SqlCommand(
              "SELECT gameStorage FROM eGame", connection);

            // Writes the BLOB to a file (*.swf).
            FileStream stream;
            // Streams the BLOB to the FileStream object.
            BinaryWriter writer;

            // Size of the BLOB buffer.
            int bufferSize = 100;
            // The BLOB byte[] buffer to be filled by GetBytes.
            byte[] outByte = new byte[bufferSize];
            // The bytes returned from GetBytes.
            long retval;
            // The starting position in the BLOB output.
            long startIndex = 0;



            // Open the connection and read data into the DataReader.
            connection.Open();
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);

            while (reader.Read())
            {


                // Create a file to hold the output.
                stream = new FileStream(
                    //"logo" + pubID + ".swf", FileMode.OpenOrCreate, FileAccess.Write);
                "FBIS" + ".swf", FileMode.OpenOrCreate, FileAccess.Write);
                writer = new BinaryWriter(stream);

                // Reset the starting byte for the new BLOB.
                startIndex = 0;

                // Read bytes into outByte[] and retain the number of bytes returned.
                retval = reader.GetBytes(0, startIndex, outByte, 0, bufferSize);




                // Continue while there are bytes beyond the size of the buffer.
                while (retval == bufferSize)
                {

                    writer.Write(outByte);
                    writer.Flush();

                    // Reposition start index to end of last buffer and fill buffer.
                    startIndex += bufferSize;
                    retval = reader.GetBytes(0, startIndex, outByte, 0, bufferSize);
                }

                // Write the remaining buffer.
                writer.Write(outByte, 0, (int)retval - 1);
                writer.Flush();

                // Close the output file.
                writer.Close();
                stream.Close();

            }

            // Close the reader and the connection.
            reader.Close();
            connection.Close();
            swfFileName = Directory.GetCurrentDirectory();







        }
    }
    catch (Exception exs) { }
}

2 个答案:

答案 0 :(得分:0)

使用FILESTREAM阅读SqlDataReader的内容效率很低,即使使用CommandBevavior.SequentialAccess也是如此。您正在执行缓冲副本,但请考虑使用Stream.CopyTo。通过SqlDataReader声明一个流是微不足道的,请参阅Download and Upload images from SQL Server via ASP.Net MVC获取这样做的示例代码。

总的来说,使用FILESTREAM blob,使用SqlFileStream要好得多,特别是对于大内容。有关完整示例,请参阅FILESTREAM MVC: Download and Upload images from SQL Server,包括通过ASP.Net下载和上载大型BLOB。

作为旁注,根据访问频率,读取文件只是为了返回此临时文件可能非常昂贵。

答案 1 :(得分:0)

要在页面中显示SWF文件,您需要一个flash对象来加载它。然后,您应该将flash对象指向ASHX处理程序,该处理程序将从数据库中获取该文件并将其返回。