Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to Upload an Image to Ftp server in Xamarin.forms?

MjDhaneshMjDhanesh INMember ✭✭

My need is to upload an image to the ftp server(ftprequest) using Xamarin.forms(Shared Project),I checked a lot but i don't get a Proper Way for Uploading,
**SOLUTION PLEASE* :)

Answers

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Hi @MjDkMJ,

    using (var client = new HttpClient())
        {
            var imageContent = new MultipartFormDataContent();
            imageContent.Add(new StringContent(IMAGE_DATA_CONVERTED_AS_BASE64_STRING), "ImageData");
    
            var httpClient = new HttpClient();
            var imageResponse = 
            await httpClient.PostAsync(YOUR_UPOLOAD_SERVICE_URL_HOSTED_ON_FTP_SERVER, imageContent);
    
            var resultImageContent = imageResponse.Content.ReadAsStringAsync().Result;
    
            if (resultImageContent!= null)
            {
                //SHOW SOME SUCCESS MESSAGE
            }
        }
    

    First convert your image as base64 string and then upload it via a http request on the ftp server, you must note that the service (end-point) must on the same ftp server.

    Hope it helps.

    -- N Baua

  • MjDhaneshMjDhanesh INMember ✭✭

    Hii Mr. @N_Baua

    I try this to convert to base64 but there is an issue regarding this

    FileName="D:\Users\MVC\wallpaper.jpg";
    byte[] b = System.IO.File.ReadAllBytes(FileName);
    String s = Convert.ToBase64String(b);

    then i use below code But Still having lots of issues, Solution Please

    using (var client = new HttpClient())
    {
    var imageContent = new MultipartFormDataContent();
    imageContent.Add(new StringContent(s), "ImageData");

        var httpClient = new HttpClient();
        var imageResponse = 
        await httpClient.PostAsync(ftp://myftserver.com/s, imageContent);
    
        var resultImageContent = imageResponse.Content.ReadAsStringAsync().Result;
    
        if (resultImageContent!= null)
        {
            //SHOW SOME SUCCESS MESSAGE
        }
    }
    

    SOLUTION PLEASE

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @MjDkMJ,

    I guess you're concepts are unclear for file uploading.

    How come you're saying D:\users\whateverpath when you want to upload an image from device to the ftp server!!!!
    if that all you want.

    Also I am not proposing to upload the file directly on the ftp server (using an ftp protocol), rather I want you to create a http service which will be accessing (and hosted on) your FTP server.

    first get your stuff wrapped up correctly or requirement and implementation front, then ask a question based on your work done + issues faced. No tailor-made solutions are available on this forum yet and don't expect people on forum to work for you either.

  • MjDhaneshMjDhanesh INMember ✭✭

    I am new in this Technology , will you please tell what is "ImageData" please);

  • MjDhaneshMjDhanesh INMember ✭✭
    edited August 2017

    and where we set username ,password and the port number of the ftp server.. @N_Baua

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @MjDkMJ said:
    I am new in this Technology , will you please tell what is "ImageData" please);

    ImageData is a Key to StringContent passed as Base64 string, it can be any name which your service can refer on server (i.e. upload service).

    var stream = IMAGE_FILE_FROM_DEVICE.GetStream();
    var bytes = new byte [stream.Length];
    await stream.ReadAsync(bytes, 0, (int)stream.Length);
    string IMAGE_DATA_CONVERTED_AS_BASE64_STRING = System.Convert.ToBase64String(bytes);
    

    so whatever file you get you need to up-stream it to the server.

  • RavinderJangraRavinderJangra USMember ✭✭✭

    I think you should use web api to upload the image and then store the image into ftp from web api.

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @MjDkMJ said:
    and where we set username ,password and the port number of the ftp server.. @N_Baua

    No need to FTP server, if your service is a secure REST over http protocol.
    like shown here

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @RavinderJangra said:
    I think you should use web api to upload the image and then store the image into ftp from web api.

    Why two operations?
    Let the web API or REST service be on the server.

  • RavinderJangraRavinderJangra USMember ✭✭✭

    I think you should use web api to upload the image and then store the image into ftp from web api.> @N_Baua said:

    @RavinderJangra said:
    I think you should use web api to upload the image and then store the image into ftp from web api.

    Why two operations?
    Let the web API or REST service be on the server.

    Yes, this can be done but in the bigger application usually, files are hosted on file server. But if it can be hosted on the same server then there is no issue.

  • MjDhaneshMjDhanesh INMember ✭✭

    but i searched the results shows uwp won't support ftprequest class?

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @MjDkMJ said:

    Thats why, I am sating forget the FTP, use HTTP, Just refer to the, code once, Hope you get it right on time.

Sign In or Register to comment.