Hi!
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 = "http://10.0.2.2:5000/api/UploadPicture";
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())
{
formData.Add(fileStreamContent);
var response = await client.PostAsync(pictureUrl, formData);
if(response.IsSuccessStatusCode)
{
}
}
}
and the VM command method:
<pre> async Task UploadImage()
{
await CrossMedia.Current.Initialize();
var mediaOptions = new PickMediaOptions()
{
PhotoSize = PhotoSize.Medium
};
var selectedImageFile = await CrossMedia.Current.PickPhotoAsync(mediaOptions);
using (var memoryStream = new MemoryStream())
{
selectedImageFile.GetStream().CopyTo(memoryStream);
Picture = memoryStream.ToArray();
await service.UploadPictureAsync(selectedImageFile,selectedImageFile.ToString());
}
if (selectedImageFile == null)
{
await Application.Current.MainPage.DisplayAlert("Error", "Could not get file", "ok");
return;
}
}
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...