locked
POST image to server RRS feed

  • Question

  • User303834 posted

    I'm pretty new to xamarin i'm trying to make an application that needs to upload an image from local storage to a webservice in PHP, i don't know if i need to save the image in a directory like '/var/www/uploads/' or convert it to a BLOB and save my image in my MySql DB. I'm using PCL solution.

    Wednesday, April 19, 2017 3:19 PM

Answers

  • User205607 posted

    You can use this plugin to select an image on the device (see usage on the README.md):

    https://github.com/jamesmontemagno/MediaPlugin

    The file is type MediaFile.

    Then, install Microsoft.Net.Http from Nuget to your PCL and platform projects. You can do the upload like this:

    ``` private MediaFile _image;

    // code here to assign image to _image

    var content = new MultipartFormDataContent(); content.Add(new StreamContent(image.GetStream()), "\"file\"", $"\"{image.Path}\"");

    var httpClient = new System.Net.Http.HttpClient(); var url = "http://upload.here.io/folder/subdir"; var responseMsg = await httpClient.PostAsync(url, content);

    var remotePath = await responseMsg.Content.ReadAsStringAsync();

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 20, 2017 2:11 AM

All replies

  • User205607 posted

    You can use this plugin to select an image on the device (see usage on the README.md):

    https://github.com/jamesmontemagno/MediaPlugin

    The file is type MediaFile.

    Then, install Microsoft.Net.Http from Nuget to your PCL and platform projects. You can do the upload like this:

    ``` private MediaFile _image;

    // code here to assign image to _image

    var content = new MultipartFormDataContent(); content.Add(new StreamContent(image.GetStream()), "\"file\"", $"\"{image.Path}\"");

    var httpClient = new System.Net.Http.HttpClient(); var url = "http://upload.here.io/folder/subdir"; var responseMsg = await httpClient.PostAsync(url, content);

    var remotePath = await responseMsg.Content.ReadAsStringAsync();

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 20, 2017 2:11 AM
  • User363904 posted

    hi dear,

    I'm looking for how to upload a MediaFile from Montemagno's MediaPlugin to my rest service too.

    My method to upload photo is there => myAddress/myWS/api/Home/upload and usually I use it like this: myAddress/myWS/api/Home/upload?= + data data.

    Can you please suggest me what I have to change in the above code to send a MediaFile to my address?

    Many thanks.

    Friday, January 26, 2018 4:01 PM
  • User383147 posted

    How would you assign an image that is stored on the local device to the _image variable?

    I have the path to the images stored within a SQLLite database but I can't figure out how to retrieve the image itself

    Tuesday, March 5, 2019 4:49 PM
  • User396720 posted

    hi i'm trying the code but i m getting a error. System.NullReferenceException Object reference not set to an instance of an object on following line; content.Add(new StreamContent(mediaFile.GetStream()), "\"file\"", $"\"{mediaFile.Path}\"");

    I'm new in xamarin forms plz help...

    Wednesday, September 9, 2020 4:09 AM
  • User396720 posted

    i didn't get any error but file is not transferring and the in remotepathlabel.text shows me a code not a file name

    Wednesday, September 9, 2020 11:30 AM
  • User396720 posted

    can you help me for PCL> @mikeacosta said:

    You can use this plugin to select an image on the device (see usage on the README.md):

    https://github.com/jamesmontemagno/MediaPlugin

    The file is type MediaFile.

    Then, install Microsoft.Net.Http from Nuget to your PCL and platform projects. You can do the upload like this:

    ``` private MediaFile _image;

    // code here to assign image to _image

    var content = new MultipartFormDataContent(); content.Add(new StreamContent(image.GetStream()), "\"file\"", $"\"{image.Path}\"");

    var httpClient = new System.Net.Http.HttpClient(); var url = "http://upload.here.io/folder/subdir"; var responseMsg = await httpClient.PostAsync(url, content);

    var remotePath = await responseMsg.Content.ReadAsStringAsync();

    ```

    How to use PCL and i didn't get a error but not upload a imaege. Plz help

    Friday, September 11, 2020 11:07 AM