简体   繁体   English

如何从 xamarin 表单应用程序将图像上传到服务器

[英]How to upload image to server from xamarin forms app

I'm trying to send an image from my xamarin forms application to a asp .net core server using a post request.我正在尝试使用 post 请求将图像从我的 xamarin 表单应用程序发送到 asp .net 核心服务器。 I need to save the image in some server folder but I can not do it.我需要将图像保存在某个服务器文件夹中,但我不能这样做。

This is the method to send the image once I have it selected in _mediaFile这是在 _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);
    }

I currently have this in my api controller我目前在我的 api 控制器中有这个

    [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 });
    }

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.我尝试应用此https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads,但它旨在直接从 asp net core 应用程序保存图像。 Can someone help me?有人可以帮助我吗? Thanks谢谢

In the end I got it!最后我明白了! What I needed was to send an image and save it to a server inside wwwroot folder.我需要的是发送图像并将其保存到 wwwroot 文件夹内的服务器。 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();
    }

The image is saved in /Uploads/UsersImg/ folder inside wwwroot folder.图像保存在 wwwroot 文件夹内的 /Uploads/UsersImg/ 文件夹中。 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!!!谢谢!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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