Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want split value base64string from image in mobile and send base64string to my database because database field text only save 65,000 string. So i want save value base64string in two field in my database.Why i want save in two field in my database because value base64string from image in storage phone more 65,000 string.Could you help me how to solve this problem, Thank you

What I have tried:

This is my coding already save only 65rb value base64, how to split save value base64 in 2 field database
async void SavePicture(object sender,EventArgs e)
     {
        file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions
        {
            PhotoSize = PhotoSize.Medium,
            CompressionQuality = 100,
        });

        //Convert image to string
        FileStream fs = new FileStream(file.Path, FileMode.Open, FileAccess.Read);
        byte[] ImageData = new byte[fs.Length];
        fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
        fs.Close();
        string imgResized = Convert.ToBase64String(ImageData);

        //Convert string to image
        Byte[] ImageFotoBase64 = System.Convert.FromBase64String(imgResized);
        ImageTest = ImageSource.FromStream(() => new MemoryStream(ImageFotoBase64));

        imgResize.Source = ImageTest;

        api = RestService.For<ApiInterface>("http://192.168.0.195/webservice/webservice.asmx");
            
        String iduser = "";

        var stringimage = imgResized;

        User user = new User(iduser);
        user.Profile_Image = stringimage;

        var response = await api.AddNewGoogle(new model.GoogleQuery(google));

        if (response.isSuccess)
        {
           Loading.toast("Sukses Menyimpan Foto");
        }
        else
        {
          LoadingFailed.toast("Gagal Menyimpan Foto");
        }

      }
Posted
Updated 16-Dec-19 20:13pm

1 solution

You can do it - it's not even complicated - but you may find you need more than one field: two fields will only hold 2* 64K = 128K and it;s very easy for a modern image to exceed that.
Instead of reading the whole image:
C#
fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
Just read the max size of your DB block converted to Base 64:
l = 4*(n/3)  (rounded up a four byte boundary)

n is the input block size, and l = 64K so:
n = 3 * 65536 / 4 == 49152

So to read a block:
C#
fs.Read(ImageData, 0, 49152);
Convert that to Base64 and store it, then read the second section:
C#
fs.Read(ImageData, 49152, 49152);
And repeat until you run out of data.

But a much better solution is to store it in a binary field that takes the whole file - every DB I know of allows for that - and convert it base64 if you need it later for presentation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900