Method Community

 

Update File API Call convert Image to byte array.

Last post 07-18-2017 6:21 PM by dknoben. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 07-17-2017 5:39 PM

    Update File API Call convert Image to byte array.

    Has anyone had to make a byte array out of an image url so they can upload it to method using the UpdateFile Call in PHP.  I am running into issues creating the image byte array and was hoping someone else migh have already solved this issue.

  • 07-18-2017 12:25 PM In reply to

    • Matt
    • Top 10 Contributor
    • Joined on 11-04-2008
    • Posts 877

    Re: Update File API Call convert Image to byte array.

    I've done it in C#, but not php.

    The first thing to do is remove "data:image/jpeg;base64," from the image string. 

    Then convert it to a byte array. In C# this looks like : byte[ uploadedImage = Convert.FromBase64String(imageString); I would assume PHP has a similar funtion.


    Matt Raiser

    Founder,



    www.techinthefield.com

    matt@techinthefield.com

    219-221-9500



    ~ Method user / partner since 2008

    ~ Over 15 years field service experience

    ~ 2013 Method Partner of the Year

    ~ Degree in Computer Science





    Creator of:


        


  • 07-18-2017 6:21 PM In reply to

    Re: Update File API Call convert Image to byte array.

    The solution for this ended up being fairly simple. The image data can be fetched using this basic pattern:
    $handle = fopen($imageUrl, 'rb');
    $imageData = stream_get_contents($handle);
    At this point, $imageData can be passed to Method using the MethodAPIUpdateFile SOAP call. However, we ran into a couple of issue which are probably worth noting:
    1) The MethodAPIUpdateFile expects a strFileName parameter which isn't mentioned in the API documentation.
    2) After some unsuccessful attempts to update a file with improperly formatted data, all subsequent MethodAPIUpdateFile calls which used the same strFileName value failed to update the file with properly formatted data, despite a success response from the SOAP service. Seems like a potential bug with the API, which we circumvented by updating strFileName to a unique value.

Page 1 of 1 (3 items)