c# - 为什么这个 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);
}
}
}
解决方案
推荐阅读
- ios - 将 KeyPaths 存储在 Dictionary/HashMap 中
- php - MYSQL 更新查询随机耗时较长
- python - 如何在 Spyder 中读取/写入文件
- r - r:如何使函数返回由 par() 排列的两个绘图对象
- odata - 用于删除预订联系人的 Odata 提取
- python - 在通过 MATLAB API for Python 时,方矩阵展平为行向量
- docker - 如何让快速网关在 Kubernetes 中工作?
- android - Jetpack Compose 中的模糊
- react-native - 我无法安装胜利本机 npm 我胜利本机
- java - 如何提高过滤速度?