简体   繁体   中英

How to save an image in a folder in ASP.NET Core MVC that comes from Xamarin.Forms Android?

I'm trying to save an image that comes from Xamarin forms Android but the method Post doesn't get the image, but the image reaches the page.

Xamarin code:

await CrossMedia.Current.Initialize();               

if (!CrossMedia.Current.IsPickPhotoSupported)
{
    await DisplayAlert("No pick photo", "no pick photo available", "OK");
}

_mediaFile = await CrossMedia.Current.PickPhotoAsync();

if (_mediaFile == null) 
    return;

lblStatus.Text = _mediaFile.Path;

var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()), "\"file\"",
                $"\"{_mediaFile.Path}\"");

var httpClient = new HttpClient();
var uploadServiceBaseAddress = "http://url/api/Files/Upload";

var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
lblStatus.Text = await httpResponseMessage.Content.ReadAsStringAsync();

ASP.NET Core MVC / C# code:

[Route("api/Files/Upload")]
public async Task<string> Post(List<IFormFile> files)
{
        try
        {
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    var fileName = formFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();
                    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "~/Uploads/", fileName);
                 
                    using (var stream = System.IO.File.Create(filePath))
                    {
                        await formFile.CopyToAsync(stream);
                    }

                    return "/Uploads/" + fileName;
                }
            }
        }
        catch (Exception exception)
        {
            return exception.Message;
        }

        return "no files";
}

In my mobile app return "no files", so the image is not saved in the folder.

Does someone know why that is?

Thank you very much

I found the solution. I leave my code in case it helps someone. I just had to change these lines

[Route("api/Files/Upload")]
    public async Task<string> Post()
    {
        try
        {
            var fileName = this.Request.Form.Files[0].FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Uploads\", fileName);

            using (var stream = System.IO.File.Create(filePath))
            {
                        await this.Request.Form.Files[0].CopyToAsync(stream);
            }
            return "/Uploads/" + fileName;

        }
        catch (Exception exception)
        {
            return exception.Message;
        }
        return "no files";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM