0

I have converted an image to a byte array using below code to store it in Database

        if (Request.Content.IsMimeMultipartContent())
            {
                var provider = new MultipartMemoryStreamProvider();
                await Request.Content.ReadAsMultipartAsync(provider);
                foreach (var file in provider.Contents)
                {
                    var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
                    var attachmentData = new AttachmentData ();

                    attachmentData.File = file.ReadAsByteArrayAsync().Result;
                    db.AttachmentData.Add(attachmentData);
                    db.SaveChanges();
                    return Ok(attachmentData);                      
                }
            }

Here File column in DB is of type "varbinary(max)" and in EF model it is byte array (byte[]).

Using above code I was able to save the image in the File column something similar to "0x30783839353034453437304430413143136303832....." (This length is exceeding 43679 characters which is more than default given to any column so the data got truncated while storing it)

I have to change the default length(43679) of column to store it in database.

Am I doing it the correct way to retrieve and store image in Database. I was also thinking to store the image as "Base64 String" but 43679 will still exceed.

I am using Angular JS to show the image on front end which uses WebAPI to fetch and save the image as ByteArray in database.

CuriousBuddy
  • 179
  • 1
  • 8
  • 21
  • How about saving the image file somewhere else (Azure blob storage, file storage on your server, somewhere else) and saving the URL to that file in the database? That'll also take some workload off your server once people start requesting files. – Rik The Developer May 12 '16 at 08:25
  • Closed. An image is nothing more than binary data and that has been answered. Your error report also shows a TON of ignorance (the limit on varbinary(max) is 2gb, not around 43k, and string base64 is significantly larger than binary) to basic facts. There is nothing complex in storing binary data via EF, outside possibly the performance (EF does not allow any streaming which means more memory usage than smarter programming via sp's or SQL to append data in chunks). – TomTom May 12 '16 at 08:31
  • Side-note: You're already running in an async context in the code you've shown - so why are you risking a deadlock by accessing `Result` on `ReasAsByteArrayAsync` rather than `await`ing it? – Damien_The_Unbeliever May 12 '16 at 08:32
  • @Damien_The_Unbeliever Thanks, I have chnage the code to " attachmentData.File = await file.ReadAsByteArrayAsync();" now. What about rest? Do you think it is the right way to store information to DB? – CuriousBuddy May 12 '16 at 09:06
  • @TomTom I had seen that link , I am not using MVC ,or, similar pattern so cannot get "System.Drawing.Image" object in action method to retrieve Image details to convert. I get image in Request body since I am using AngularJS – CuriousBuddy May 12 '16 at 09:10

1 Answers1

0

Yes, It really helps to not know how databases work.

First:

varbinary(max)

This stores up to 2gb, not 43679 bytes.

Then:

similar to "0x30783839353034453437304430413143136303832.....

This is not how it is stored. This is a textual representation in uotput.

(This length is exceeding 43679 characters which is more than default given to any column so the data got truncated while storing it)

There is no default given to any column - outside basically SQL Server Management Studio which likely will not be able to handle images as images and has a lot of limitations. But this is not the database, it is an admin ui.

I was also thinking to store the image as "Base64 String" but 43679 will still exceed.

Actually no, it will exceed this by more - your data will be significantly longer as Base64 is longer than binary data.

TomTom
  • 61,059
  • 10
  • 88
  • 148