Click here to Skip to main content
14,775,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

I have a xamarin application where i want to upload an image into a folder on the server-side.Everything seems to be working properly except for the fact that the image won't save.I checked the api and it's ok,I checked the server-side,the method alone works fine using Postman as it uploads the image in the folder but when i try to upload an image from the client-side,it gets lost somewhere along the way.I have no error what so ever that could point me to a possible mistake in my code as the image is taken from the android emulator,converted into a byte array,stored into the database,but nowhere to be found in the folders' server side.
This is my Model class:
<pre> public  class User
        public int IdUser { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string ConfPassword { get; set; }
        public DateTime Created { get; set; } = DateTime.Now;
        public DateTime? Dob { get; set; }
        public byte[] Picture { get; set; }

        public virtual ICollection<Login> Login { get; set; }

Server-side controller api method:
<pre>  [HttpPost]
        public async Task Post(IFormFile file)
            if (string.IsNullOrWhiteSpace(_environment.WebRootPath))
                _environment.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");

            var uploads = Path.Combine(_environment.WebRootPath, "uploads");

            if (!Directory.Exists(uploads)) Directory.CreateDirectory(uploads);

            if (file.Length > 0)
                using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create))
                    await file.CopyToAsync(fileStream);

client-side web service:
<pre>  public async Task UploadPictureAsync(MediaFile image,string fileName)
           string pictureUrl = "";
            HttpContent fileStreamContent = new StreamContent(image.GetStream());
            fileStreamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "file", FileName = fileName };
            fileStreamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
            HttpClientHandler clientHandler = new HttpClientHandler();
            clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
            using (var client = new HttpClient(clientHandler))
            using (var formData = new MultipartFormDataContent())
                var response = await client.PostAsync(pictureUrl, formData);

and the VM command method:
<pre> async Task UploadImage()
            await CrossMedia.Current.Initialize();
        // await CrossPermissions.Current.RequestPermissionAsync<CameraPermission>();
            var mediaOptions = new PickMediaOptions()
                PhotoSize = PhotoSize.Medium
            var selectedImageFile = await CrossMedia.Current.PickPhotoAsync(mediaOptions);
            using (var memoryStream = new MemoryStream())
                Picture = memoryStream.ToArray();
               await service.UploadPictureAsync(selectedImageFile,selectedImageFile.ToString());
            if (selectedImageFile == null)

                await Application.Current.MainPage.DisplayAlert("Error", "Could not get file", "ok");

Did anyone face this kind of situation and knows how it can be resolved?Any help would be appreciated.

What I have tried:

I have not tried anything yet since i have no idea what the issue might be...
Updated 30-Jul-20 6:36am
Gerry Schmitz 30-Jul-20 14:46pm
Get it to work in a console app first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900