如何将Mp3文件在数据库中另存为二进制数据? [英] How Can I Save Mp3 File in DataBase As Binary Data ?

查看:66
本文介绍了如何将Mp3文件在数据库中另存为二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Mp3文件,要通过将其转换为二进制数据将其保存为数据库中的字符串,怎么办?
我使用Windows应用程序,并将使用Access数据库存储文件.

I have Mp3 File want to save it in database as string by convert it to binary data how do that ??
i use windows application and will use access database for store files

推荐答案

您可以将它们存储为BLOB.但是,您应该考虑将.mp3文件存储在服务器上并将其路径存储在数据库中.
You can store them as BLOBs. However, you should consider storing the .mp3 files on the server and storing their paths in the database.


首先,您需要将文件加载到流中.

First you need to load the file into a stream

FileStream fileStream = new FileStream(@"d:\\Del\7\\SS 65.mp3", FileMode.Open);




然后将其转换为字节[]




Then convert it into a byte[]

byte[] mp3Byte = Mario.streamToByteArray(fileStream,1024)


);


这是streamtobytearray方法


);


here is the streamtobytearray method

public static byte[] streamToByteArray(Stream stream, int initialLength)
    {
      // If we've been passed an unhelpful initial length, just
      // use 32K.
      if (initialLength < 1)
      {
        initialLength = 32768;
      }
      byte[] buffer = new byte[initialLength];
      int read = 0;
      int chunk;
      while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
      {
        read += chunk;
        // If we've reached the end of our buffer, check to see if there's
        // any more information
        if (read == buffer.Length)
        {
          int nextByte = stream.ReadByte();
          // End of stream? If so, we're done
          if (nextByte == -1)
          {
            return buffer;
          }
          // Nope. Resize the buffer, put in the byte we've just
          // read, and continue
          byte[] newBuffer = new byte[buffer.Length * 2];
          Array.Copy(buffer, newBuffer, buffer.Length);
          newBuffer[read] = (byte)nextByte;
          buffer = newBuffer;
          read++;
        }
      }
      // Buffer is now too big. Shrink it.
      byte[] ret = new byte[read];
      Array.Copy(buffer, ret, read);
      return ret;
    }



然后将其作为字段发送到数据库



Then send it to the db as a field


这篇关于如何将Mp3文件在数据库中另存为二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆