Forum Xamarin.Forms

Post Data API with Upload Image and data

Dear Techie,
I have an query from API's . I have an functionality to post an image and name , place in to the database. but i dont couldn't achieve in C# api .

Can you guys guide me .

Thanks in advance,
Vasanth

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    but i dont couldn't achieve in C# api

    At which step did you encounter a problem? Could you post more details about the issue?

    How did you load the images? You could store the images to the database as a byte[].

    public class TodoItem
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public byte[] itemData { get; set; }
    }
    

    Download the api to get the byte array and convert it to the ImageSource.

    var imageBytes = await downloadImage();
    image.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));
    
  • Vasanthakumar06Vasanthakumar06 Member ✭✭
    edited November 2020

    if (!Request.Content.IsMimeMultipartContent())
    {
    return StatusCode(HttpStatusCode.UnsupportedMediaType);
    }
    var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();
    var jsonkey = await filesReadToProvider.Contents[0].ReadAsStringAsync();
    var fileBytes = await filesReadToProvider.Contents[0].ReadAsByteArrayAsync();
    var data = jsonkey + fileBytes;
    return Ok(data);

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited November 2020

    @Vasanthakumar06 said:
    if (!Request.Content.IsMimeMultipartContent())
    {
    return StatusCode(HttpStatusCode.UnsupportedMediaType);
    }
    var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();
    var jsonkey = await filesReadToProvider.Contents[0].ReadAsStringAsync();
    var fileBytes = await filesReadToProvider.Contents[0].ReadAsByteArrayAsync();
    var data = jsonkey + fileBytes;
    return Ok(data);

    What's wrong with the code? Please post details about that.

  • Breakpoint is not hitting var jsonkey . and when readAsMultipartAsync () its showing error

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    The breakpoint cannot be hit because of an error in 'readAsMultipartAsync'. What error occured? Please post the error log.

Sign In or Register to comment.