首页 > 解决方案 > 为什么这个 ASP.NET Core 代码不能在远程主机上运行

问题描述

我有一个可以在我的本地主机上运行的 Angular 网站。当我将它上传到远程主机时,它会失败而没有错误。只是不保存任何文件。

我已经在网上搜索了答案。没有喜悦。

这永远不会在本地 Visual Studio 2019 或远程返回错误。

在 Microsoft 网站上找到基本代码。看了很多有角度的网站

[Route("api/[controller]")]
[ApiController]
public class UploadController : ControllerBase
{
    private IHostingEnvironment env;

    public UploadController(IHostingEnvironment hostingEnvironment)
    {
        env = hostingEnvironment;
    }

    [HttpPost, DisableRequestSizeLimit]
    public async Task<IActionResult> Upload()
    {
        try
        {
            var files = Request.Form.Files;

            if (files.Count < 1)
            {
                return BadRequest();
            }

            if (files.Any(f => f.Length == 0))
            {
                return BadRequest();
            }

            foreach (var file in files)
            {
                string folderName = $"Project_{file.FileName}";
                string webRootPath = env.WebRootPath;
                string newPath = Path.Combine(Startup.documentPath, folderName);
                if (!Directory.Exists(newPath))
                {
                    Directory.CreateDirectory(newPath);
                }

                if (file.Length > 0)
                {
                    string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).Name.Trim('"');
                    string fullPath = Path.Combine(newPath, fileName);

                    if (System.IO.File.Exists(fullPath))
                    {
                        System.IO.File.Delete(fullPath);
                    }

                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                         await file.CopyToAsync(stream);
                    }
                }
            }

            return Ok("All the files are successfully uploaded.".csConverToJSON());
        }
        catch (Exception ex)
        {
            return StatusCode(500, ex.Message);
        }
    }
}

标签: c#asp.net-core

解决方案


推荐阅读