Upload recently taken Image from Android/iOS (Cros

2019-08-20 20:30发布

问题:

currently I'm stuck on uploading the images which I took within my app on my own asp.net webserver. I used the Media.Plugin from James Montemagno. Taking the image works fine.

The problem: I'm getting a unhandled exception when uploading the image.

In my solution I created a new WebApplication as referenced in the youtube tutorial! I did almost everything like him.

Here is my WebApplication Code:

public async Task<string> Post()
        {
            try
            {
                var httpRequest = HttpContext.Current.Request;
                if (httpRequest.Files.Count > 0)
                {
                    foreach (string file in httpRequest.Files)
                    {
                        var postedFile = httpRequest.Files[file];
                        var fileName = "test.png";
                            // postedFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();
                        var filePath = HttpContext.Current.Server.MapPath("~/Uploads/" + fileName);
                        postedFile.SaveAs(filePath);
                        return null;
                    }
                }
            } catch (Exception exception)
            {
                return exception.Message;
            }
            return null;
        }

Here is my code for taken the images and uploading it:

 private async void listViewJson_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            {
                await CrossMedia.Current.Initialize();

                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    await DisplayAlert("No Camera", ":( No camera available.", "OK");
                    return;
                }

                for (int i = 0; i < 2; i = i + 1)
                {
                    DateTime dt = DateTime.Now.AddHours(2);
                    var format = "MM/dd/yyyy_hh:mm:ss";
                    String curDateTime = dt.ToString(format);
                    var process_fullname = ((Process)(e.Item)).id.ToString() + "-" + curDateTime;

                    var _mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                    {
                        Directory = "Sample",
                        Name = $"{process_fullname}.jpg",
                        PhotoSize = PhotoSize.Small,
                        SaveToAlbum = true
                    });

                    if (_mediaFile == null)
                        return;

                    var image = new Image();
                    image.Source = ImageSource.FromStream(() =>
                    {
                        return _mediaFile.GetStream();
                    });

                    await DisplayAlert("ok", _mediaFile.AlbumPath, "ok");

                    try
                    {
                        var content = new MultipartFormDataContent();
                        content.Add(new StreamContent(_mediaFile.GetStream()), 
                            "\"file\"", 
                            $"\"{_mediaFile.AlbumPath}\"");
                        var httpClient = new HttpClient();
                        var uploadServiceBaseAddress = "http://localhost:53188/";
                        var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
                        var remotePath = await httpResponseMessage.Content.ReadAsStringAsync();
                    }
                    catch (Exception exception)
                    {
                        throw exception;
                    }
                }
            };
        }

Any help is really appreciated.

Thanks in advance, Paul