如何从Xamarin Forms应用程序将图像上传到服务器 [英] How to upload image to server from xamarin forms app

查看:550
本文介绍了如何从Xamarin Forms应用程序将图像上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用发布请求将图像从xamarin表单应用程序发送到asp .net核心服务器.我需要将图像保存在某些服务器文件夹中,但无法执行.

I'm trying to send an image from my xamarin forms application to a asp .net core server using a post request. I need to save the image in some server folder but I can not do it.

这是在_mediaFile中选择图像后发送图像的方法

This is the method to send the image once I have it selected in _mediaFile

    private async void UploadFile_Clicked(object sender, EventArgs e)
    {
        var uri = new Uri(string.Format(Constants.UsersRestUrl + "/Files/Upload/", string.Empty));
        var content = new MultipartFormDataContent();

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

        var httpClient = new HttpClient();
        var httpResponseMessage = await httpClient.PostAsync(uri, content);
    }

我目前在我的api控制器中有这个

I currently have this in my api controller

    [Route("Files/Upload/")]
    [HttpPost]
    public async Task<IActionResult> Post(IFormFile file)
    {
        Debug.Write("******");
        // full path to file in temp location
        var filePath = Path.GetTempFileName();
        Debug.Write("****** File Path " + filePath);

        if (file.Length > 0)
        {
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }
        }
        return Ok(new { file });
    }

我尝试应用此 https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/file-uploads ,但其目的是直接从ASP网络核心应用程序保存图像. 有人能帮我吗?谢谢

I tried to apply this https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads but it is oriented to save the image directly from an asp net core application. Can someone help me? Thanks

推荐答案

最后我明白了!我需要发送图像并将其保存到wwwroot文件夹中的服务器.我在这里留下了服务器控制器的方法,以防将来有人需要它.

In the end I got it! What I needed was to send an image and save it to a server inside wwwroot folder. I leave here the method for server controller in case someone needs it in the future.

    [Route("Files/Upload/")]
    [HttpPost]
    public async Task<IActionResult> Post(IFormFile file)
    {
        //Windows path
        var uploadLocation = Path.Combine(_env.WebRootPath, "Uploads\\UsersImg");

        //Linux path
        //var uploadLocation = Path.Combine(_env.WebRootPath, "Uploads//UsersImg");

        var fileName = file.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();

        if (file.Length > 0)
            {
                using (var stream = new FileStream(Path.Combine(uploadLocation, fileName), FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }
            }
        return Ok();
    }

图像保存在wwwroot文件夹内的/Uploads/UsersImg/文件夹中. 从客户端向服务器发送图像的方法与我在问题中发布的方法相同.

The image is saved in /Uploads/UsersImg/ folder inside wwwroot folder. The method to send the image from client to server it's the same I posted in the question.

如果某人可以投票赞成其他用户的问题,那就没问题了.谢谢!!!

If someone can vote up the question to help other users would be fine. Thanks!!!

这篇关于如何从Xamarin Forms应用程序将图像上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆