0

enter image description here

here is how i get the byte array and the type of array and how i send the object

  public partial class Addprofilepicture : ContentPage
    {
        ImageButton imagebutton;
        Label basse64;
        IMyAPI myAPI;
        APIRequestHelper apiRequestHelper;
        public Addprofilepicture()
        {
            InitializeComponent();
            imagebutton = this.FindByName<ImageButton>("addprofilepicture");
            basse64 = this.FindByName<Label>("base64");
            myAPI = RestService.For<IMyAPI>("http://10.0.2.2:8080");
            apiRequestHelper = new APIRequestHelper(myAPI);
        }

        private async void addprofilepicture_Clicked(object sender, EventArgs e)
        {



            Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync();
            if (stream != null)
            {
                ImageSource Image;
                byte[] b = ReadFully(stream);
                String s = Convert.ToBase64String(b);
                Image = Xamarin.Forms.ImageSource.FromStream(
                () => new MemoryStream(Convert.FromBase64String(s)));
                imagebutton.Source = Image;
                basse64.Text = s;


                Postbase(b);

            }



        }
        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }
        public async void Postbase(byte[] bytearray) {


            base64s s = new base64s()
            {
                base64string = bytearray

            };
           // string stringpayload =  JsonConvert.SerializeObject(s);

            await myAPI.PostProfilePhoto(s);



        }
      public  class base64s { 
          public byte[] base64string { get; set; }


        }
    }

it take almost a whole minute or two so i assume there must be a faster way to send it here is the post method

[Post("/Phototest")]
    Task<string> PostProfilePhoto(base64s s);

here is the node js code

app.post('/Phototest', (request, response) =>{
  console.log('request tried');
  REQUEST_BODY = request.body;
  console.log(REQUEST_BODY.base64string);

});

the console (im showing because im not sure if this is what a byte[] should look like).(it takes forever) im new and wondering hoe to make this posting process quicker and not fail becasue its taking too long.

bytearray

  • You want to know how to upload an image to server side? – nevermore Dec 04 '19 at 07:03
  • @jackHua-MSFT yes because this sends the byte[] i think but it 1.takes forever longer then i think it should take 2. crashes the application with the error above. and idk if this is the proper way to do it. but if it is not please tell me. but either way i need to figure out to prevent the error. thanks – theconfusedone2 Dec 04 '19 at 07:29
  • and have it be from the xamarin image stream and to a common format because it will be saved to firebase storage after it gets to server so it needs to be a Uint8Array base64, base64url, or data_url according to firebase – theconfusedone2 Dec 04 '19 at 07:36
  • Have a look at these two threads may help: [POST image to server](https://forums.xamarin.com/discussion/comment/267762/#Comment_267762) and [upload-image-using-httpclient](https://stackoverflow.com/questions/27425043/upload-image-using-httpclient) – nevermore Dec 04 '19 at 07:55
  • @jackHua-MSFT but those assumei have the file.jpg or png but i start with a stream that when i attempt convert it to a string and see it in a label i get xamarin.forms.streamimagesource – theconfusedone2 Dec 04 '19 at 08:24
  • system.io.stream is what a start with not a file stream so idk – theconfusedone2 Dec 04 '19 at 08:31
  • The first link use a stream and you can have a try. – nevermore Dec 04 '19 at 09:40

0 Answers0