1

In Xamarin.Forms I am sending my Image to rest Api, while in the Add the image is not being converted as needed, getting and error cannot convert system.net.http.streamcontent to byte[]

HttpClient client = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(new StreamContent(new MemoryStream(image1)), "bilddatei", "upload.jpg"));

In the code mentioned image1 is an image taken from camera using Xamarin.Forms, please help me resolve this, or let me know of alternate options to send image to Rest web service.

Almir Vuk
  • 2,983
  • 1
  • 18
  • 22

2 Answers2

0

Looks like you need to pass a byte[] instead of a StreamContent. Try this:

var client = new HttpClient();
var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(new MemoryStream(image1).ToArray()), "bilddatei", "upload.jpg");

Update: If you're saving the picture on device's storage, you can send it like:

var client = new HttpClient();
var form = new MultipartFormDataContent();
var path = "path/to/file";
form.Add(new ByteArrayContent(File.ReadAllBytes(path)), "bilddatei", "upload.jpg");
Santiago Hernández
  • 5,438
  • 2
  • 26
  • 34
  • Hi, Thanks for your help on this, I am using the above as suggested, I am getting an error "cannot convert Xamarin.forms.image to byte[]", any suggestion on how to correct this? – Harish Velala Aug 01 '16 at 16:50
  • can you post more code? it would be helpful to know where is image1 coming from... also I just updated the answer probably that'll help – Santiago Hernández Aug 01 '16 at 17:55
  • public imageRest(Image image) { this.image = image; //image.Source = "/Assets/Image1.png"; MemoryStream stream = new MemoryStream(); ReadFully(stream); } – Harish Velala Aug 01 '16 at 22:08
  • var client = new HttpClient(); var form = new MultipartFormDataContent(); form.Add(new ByteArrayContent(new MemoryStream(image).ToArray()), "bilddatei", "upload.jpg"); – Harish Velala Aug 01 '16 at 22:09
  • 1
    image is a Xamarin.forms.image which I want to send to Rest api – Harish Velala Aug 01 '16 at 22:10
  • Can you please help me with a way to convert Xamarin.Forms.Image to Byte[] ? – Harish Velala Aug 02 '16 at 21:25
  • 1
    yeah, why don't you ask that question here in SO? we will help you but that's another (good) question. Make sure to check this resources first: [Convert Forms Image to byte array](https://forums.xamarin.com/discussion/46010/convert-image-to-byte), [Convert Image into byte array in Xamarin.Forms](http://stackoverflow.com/questions/33947138/convert-image-into-byte-array-in-xamarin-forms) – Santiago Hernández Aug 02 '16 at 22:05
0

I understood the doubt, but I believe there is a better way to do this. Using Firebase storage and writing to your database, only the image URL saved in Firebase.

Like this:

>

using (var ms = new MemoryStream(file.ByteFile))
{
    ms.Seek(0, SeekOrigin.Begin);

    var task = new FirebaseStorage(
        "your.adress.firebase.com",
         new FirebaseStorageOptions()
         {
             HttpClientTimeout = new TimeSpan(0, 30, 0)
         })
         .Child("Files")
         .Child($"{file.id}.mp4")
         .PutAsync(ms);

         file.LinkVideo = await task;
         Entity entity = new Entity()
         {
             Name = file.Name,
             LinkVideo = file.LinkVideo,
         };
         //Your API
         return await PostEntity(contest);
}