Hello, I've spent out try to figure out why my image upload keeps timing out. Everything has worked before but i seem to be struggling on something trival.
here is my post to my web api
byte[] ImageUploadBase64;
private async void Button_Clicked(object sender, EventArgs e)
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", ":( No camera available.", "OK");
return;
}
var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
using (var memoryStream = new MemoryStream())
{
file.GetStream().CopyTo(memoryStream);
var myfile = memoryStream.ToArray();
ImageUploadBase64 = myfile;
}
var check = await UploadTheImage();
}
public async Task<HttpResponseMessage> UploadTheImage()
{
Stream stream = new MemoryStream(ImageUploadBase64);
var FixedFileName = DateTime.Now.Millisecond + "PhotoDocument" + "1234" + ".jpg";
StreamContent scontent = new StreamContent(stream);
scontent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
FileName = FixedFileName,
Name = "image"
};
scontent.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
var client = new HttpClient(new NativeMessageHandler());
var multi = new MultipartFormDataContent();
multi.Add(scontent);
client.BaseAddress = new Uri("myUrl");
HttpResponseMessage result = await client.PostAsync("api/UploadImage/", multi).ConfigureAwait(false);
// var ResultResponse = await result.Content.ReadAsStringAsync();
return result;
}
and my endpoint which im just trying to save the image to disk
[HttpPost]
[Route("api/UploadImage")]
public Task ImageUpload()
{
if (!Request.Content.IsMimeMultipartContent())
{
return null;
}
else
{
string root = HttpContext.Current.Server.MapPath("~/Files/Photos");
var provider = new CustomMultipartFormDataStreamProvider(root);
var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
}
foreach (MultipartFileData uploadedFile in provider.FileData)
{
string path = provider.FileData[0].LocalFileName;
int pos = path.LastIndexOf("\\") + 1;
string GeneratedFileName = path.Substring(pos, path.Length - pos);
// var UpdateGoal = db.UserGoals.SingleOrDefault(x => x.UserID == GetUser.ID && x.UserGuid == UserGuid && x.ID == NewGoalID);
using (Image image = Image.FromFile(root + "\\" + GeneratedFileName))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
//UpdateGoal.GoalImageFile = base64String;
//UpdateGoal.GoalImageUrl = "url" + GeneratedFileName;
//UpdateGoal.GoalTypeID = 2;
//db.SaveChanges();
}
}
}
return Request.CreateResponse(HttpStatusCode.OK);
});
//task.Wait();
return task;
}
}
the task just continues to cancel after a minute, ive also tried extending the HttpClient Timeout which has no affect. I've tried with smalled sized images, and checking the image that i select is a max of 500k so its not that the image is too large.
any ideas??