简体   繁体   中英

How to Concatenate Byte Array of a Video File saved in SQL Server?

I was able to split video file binary data and save into SQL Server, the entire video size is 14 MB; But while downloading from Database its only 13MB and video is not playable.

I think my concatenation code is not correct. Any help would be nice.

Split and save code:

FileStream fs = null;
fs = new FileStream("test.mp4", FileMode.Open, FileAccess.Read);

Byte[] blob = new Byte[fs.Length];
fs.Read(blob, 0, blob.Length);
fs.Close();

byte[] smallPortion;

for (int i = 1800000; i < blob.Length; i = i + 1800000) 
{
    smallPortion = blob.Skip(i).Take(1800000).ToArray();

    cmd = new SqlCommand("INSERT INTO machineInfo (machineFile) values (@BLOBPARAM) ", conn);
    param = new SqlParameter("@BLOBPARAM", SqlDbType.VarBinary, blob.Length,ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, smallPortion); 
    cmd.Parameters.Add(param);

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}

Concatenate code:

SqlCommand command = new SqlCommand("select machineFile  from machineInfo", conn);

// Read Start
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);

byte[] productImage = new byte[0];
byte[] productImage2 = new byte[0];

foreach (DataRow dr in dt.Rows)
{
    productImage = (byte[])dr["machineFile"];
    productImage2 = productImage2.Concat(productImage).ToArray(); 
}            

byte[] buffer = productImage2; 
conn.Close();

FileStream fs = new FileStream(@"H:\test.mp4", FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Close();

There is no sorting specified in your select query, and those parts may select in a wrong order. Please create an identity column to preserve the sequence of parts which are inserted to the table. Then in select query order rows ASC by the identity column. Please do this and check I am correct or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM