表单无法使用IFormFile查找操作

时间:2019-02-18 14:53:21

标签: c# asp.net-mvc asp.net-core

我可以在本地计算机上成功上传文件,但是当我尝试在主机上上传时找不到操作。我不知道为什么这是我的代码。

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

0 个答案:

没有答案