Tried : https://forums.xamarin.com/discussion/18649/best-practice-to-upload-image-selected-to-a-web-api?
My code :
private async void upload()
{
var file = mimgname; // "path/to/file.ext";
var url = NetworkCheck.ipAddr() + "/lcappi/imgupload/imgupload.php";
try
{
System.IO.Stream fileStream = System.IO.File.Open(file, FileMode.Open);
byte[] data = ReadFully(fileStream);
fileStream.Close();
MultipartFormDataContent multi = new MultipartFormDataContent();
ByteArrayContent imageStream = new ByteArrayContent(data);
StringContent SequenceID = new StringContent(osequence);
imageStream.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
imageStream.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = osequence, // "screenshot.jpg", // generate this and send
Name = "avatar",
};
multi.Add(imageStream);
alertLabel.Text = "Uploading Now";
var response =
await App.client.PostAsync(url, multi);
string responsestr = response.Content.ReadAsStringAsync().Result;
var retresponse = new retResponse();
bool uploadSuccess = false;
if (responsestr != "")
{
retresponse = JsonConvert.DeserializeObject<retResponse>(responsestr);
if (retresponse.status2 == 1) uploadSuccess = true;
}
if (uploadSuccess)
{
alertLabel.Text = "Image uploaded successfully";
DependencyService.Get<IFileManager>().DeleteFile(file);// (file.Path);
Application.Current.MainPage.Navigation.PopAsync(); //Remove the page currently on top.
}
alertLabel.Text = "After Upload Command2 ";
if (responsestr != "") alertLabel.Text = responsestr.ToString(); else alertLabel.Text = alertLabel.Text + " After Upload Command ";
}
catch (Exception e)
{
}
} // private void upload(MediaFile mediaFile)
I am able to upload smaller images (< 570kb) . Larger ones are not getting uploaded. I checked the web api using Postman where I could upload files as large as 2 mb. So I don't this the Res APi is the issue.
I am using CrossMedia to Capture image from camera. Then I am navigating to another page to display the captured image and let user click a button to upload. The code for this as follows:
private async void ImageButton_Clicked(object sender, EventArgs e)
{
alertLabel.Text = "state1";
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
alertLabel.Text = "state1 if start";
//// DisplayAlert("No Camera", ":( No camera available.", "OK");
alertLabel.Text = "state1 if end ";
return;
}
var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "LCAPP",
//// Name = $"{DateTime.UtcNow}.jpg",
Name = $"Tempimg.jpg",
SaveToAlbum = true,
CompressionQuality = 100,
CustomPhotoSize = 50,
PhotoSize = PhotoSize.MaxWidthHeight,
MaxWidthHeight = 2000,
DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Rear
};
alertLabel.Text = "state1 after var mediaOptions ";
var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions);
alertLabel.Text = "state1 after var file ";
if (file == null)
return;
var filePath = file.Path;
// filePath = "LCAPP/*.*";
var directory = file.AlbumPath;
// DisplayAlert("File Location", file.Path, "OK");
alertLabel.Text = "state1 before var image source ";
alertLabel.Text = file.AlbumPath;
file.Dispose();
var imageparam = new ImageParams
{
imgName = filePath.ToString(),
locID = 20
};
var imagepage = new ShowImage(imageparam.imgName,opdetailSequenceForImage);
App.imgDelete = false;
await Navigation.PushAsync(imagepage);
}
Donot know how to resolve this, breaking my head