我可以在本地计算机上成功上传文件,但是当我尝试在主机上上传时找不到操作。我不知道为什么这是我的代码。
HTML
<form method="post" asp-action="UploadFile" asp-controller="Admin" enctype="multipart/form-data">
<div class="custom-file">
<input type="file" class="custom-file-input" id="file" name="file">
<label class="custom-file-label" for="file">Browse</label>
</div>
<button class="btn btn-brand" type="submit">Upload</button>
</form>
C#
public IActionResult UploadFile([FromForm]IFormFile file)
{
var path = Path.Combine(_environment.WebRootPath, "uploads");
if (file!= null && file.Length > 0)
{
using (var reader = new FileStream(Path.Combine(path, "gallery", file.FileName), FileMode.Create))
{
file.CopyTo(reader);
}
Gallery gallery = new Gallery
{
Path = file.FileName
};
db.Gallery.Add(gallery);
db.SaveChanges();
}
return RedirectToAction("Gallery");
}