Database: image file as blob or file path?

后端 未结 3 637
北荒
北荒 2021-01-20 23:54

I know this question has been asked any a times.

I read this whole post but still couldn\'t reach to a point.

I am making a website which will allow users to do

相关标签:
3条回答
  • 2021-01-21 00:27

    In My opinion you should store path in instead of blob. But again it depends upon many other things - Do you have your own server? or you are hosting it on somewhere else ? Are these things really critical to put as blob in DB ? What will be your backup strategy etc..

    I have my own reasoning

    • File system is there to manage files and in your case songs and images are not very critical. and imagine if your database will grow in size the performance will go down. If objects are larger than one megabyte on average, NTFS has a clear advantage over SQL Server

    Also keep in mind when you have to take backup of your database it may be a big headache at some stage. On the other hand if your songs file are on disk then it will be easy.

    Also if you are storing just the path in database, you can consider to keep your songs and picture on some other server (or multiple servers) as well.

    In short, I can see many advantages to use file system to store songs and pictures.

    0 讨论(0)
  • 2021-01-21 00:29

    I found another good answer by KBrocksi_SEC in forum.asp.net, so i share it with you:

    A commonly used alternative approach to handling BLOB data is to store the BLOB data in the file system, and store a pointer (preferably a Uniform Resource Locator [URL] link) in a database column to reference the appropriate file.

    Advantages of Storing BLOB Data in the Database

    Storing BLOB data in the database offers a number of advantages:

    • It is easier to keep the BLOB data synchronized with the remaining items in the row.
    • BLOB data is backed up with the database. Having a single storage system can ease administration.
    • BLOB data can be accessed through XML support in SQL Server 2005, which can return a base 64–encoded representation of the data in the XML stream.
    • SQL Server Full Text Search (FTS) operations can be performed against columns that contain fixed or variable-length character (including Unicode) data. You can also perform FTS operations against formatted text-based data contained within image fields—for example, Microsoft Word or Microsoft Excel documents.

    Disadvantages of Storing BLOB Data in the Database

    Carefully consider what resources might be better stored on the file system rather than in a database. Good examples are images that are typically referenced via HTTP HREF. This is because:

    • Retrieving an image from a database incurs significant overhead compared to using the file system.
    • Disk storage on database SANs is typically more expensive than storage on disks used in Web server farms.

    The following code shows how to use ADO.NET to write binary data obtained from a file to an image field in SQL Server.

    public void StorePicture( string filename )
    {
      // Read the file into a byte array
      using(FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
      {
        byte[] imageData = new Byte[fs.Length];
        fs.Read( imageData, 0, (int)fs.Length );
      }
    
    
      using( SqlConnection conn = new SqlConnection(connectionString) )
      {
        SqlCommand cmd = new SqlCommand("StorePicture", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@filename", filename );
        cmd.Parameters["@filename"].Direction = ParameterDirection.Input;
        cmd.Parameters.Add("@blobdata", SqlDbType.Image);
        cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input;
        // Store the byte array within the image field
        cmd.Parameters["@blobdata"].Value = imageData;
        conn.Open();
        cmd.ExecuteNonQuery();
      }
    }
    

    Also i can add another notes to the above explanations myself:

    Disadvantages of Storing BLOB Data in the Database

    • If your BLOB Data cause a heavy database (when you store many files in DB) then the database backup process maybe take more time to complete and cause headaches for you.
    0 讨论(0)
  • 2021-01-21 00:31

    it really depends on what you're doing.

    if you use file paths, then you have to have different and consistent security setup on that path. you also have to do something to enforce unique filenames, etc. you also need to set up backup for that directory as well

    if you use blobs, the database handles security for you, backup should be consistent with everything else in the db.

    you might store them as blobs in a different table, with a FK from the first table to the blob table. then you can avoid storing the same blob multiple times, etc.

    0 讨论(0)
提交回复
热议问题