2011-02-12 62 views
0

我堅持在我的aspx中加載swf。我應該從數據庫檢索瑞士法郎,並顯示在網頁上。我的代碼設法在從數據庫中檢索後在本地文件夾中創建swf ...但是我似乎無法在下載後自動在aspx上顯示它...以及aspx中的代碼應該是什麼?任何想法傢伙?... ... THX在ASP.NET中加載SWF文件

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) { } 
} 

回答

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。

作爲一個提示,爲了返回此臨時文件而讀入文件可能會非常昂貴,具體取決於訪問頻率。

0

要在頁面中顯示SWF文件,您需要一個flash對象來加載它。然後,您應該將flash對象指向ASHX處理程序,該處理程序將從數據庫獲取文件並將其返回。

+0

yup ... ASHX方法的工作原理和我使用@Remus建議的SqlFileStream修改代碼... thx傢伙的所有幫助.... – user614454 2011-02-17 15:39:12