簡體   English   中英

IFormFile 在 ASP.NET Core 3.0 中返回 NULL

[英]IFormFile is returning NULL in ASP.NET Core 3.0

我已經閱讀過與我類似的帖子,但它們沒有幫助。
所以發生的事情是我正在關注kudvenkat 的asp.net core mvc 教程系列。
我有一個簡單的表單,其中包含一些可以正常工作的字段,並且它們正在正常發送數據,但是用於照片上傳的字段不起作用。 每當我點擊提交按鈕時,應在 HomeController 中保存文件名的對象 ( model.Photo ) 的字段為null 它可能有一些從 2.1 到 3.0 版本的 asp.net core 的更改,但我只是猜測。

Create.cshtml (我只粘貼了該字段的代碼和提交按鈕,表單方法設置為 POST)

<div class="form-group">
     <label asp-for="Photo"></label>
     <div class="custom-file">
        <input asp-for="Photo" class="form-control custom-file-input" formenctype="multipart/form-data"/>
        <label class="custom-file-label">Choose file...</label>
     </div>
</div>
<div>
    <button class="btn btn-secondary" type="submit"><i class="fas fa-plus-square"></i> Create</button>
</div>

HomeController.cs (我只粘貼了這個問題的興趣代碼,如果你需要,我可以顯示其余的代碼)

public IActionResult Create(EmployeeCreateViewModel model)
        {
            if(ModelState.IsValid)
            {
                string uniqueFileName = null;
                if(model.Photo != null)
                {
                    string uploadsFolder = Path.Combine(hostingEnviroment.WebRootPath, "img");
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.Photo.FileName);
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }

                Employee newEmployee = new Employee
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    Department = model.Department,
                    Job = model.Job,
                    Address = model.Address,
                    Birthday = model.Birthday,
                    PhotoPath = uniqueFileName
                };

                _employeeRepository.Add(newEmployee);

                return RedirectToAction("Details", new { id = newEmployee.Id });
            }

            return View();
        }

員工創建視圖模型.cs

    public class EmployeeCreateViewModel
    {
        [Required]
        [Display(Name = "First name")]
        public string FirstName { get; set; }
        [Required]
        [Display(Name = "Last name")]
        public string LastName { get; set; }
        [Required]
        [RegularExpression(@"^[a-zA-Z0-9_.+-]+@coreuniverse.com",
        ErrorMessage = "Invalid email format. Follow pattern: etc@coreuniverse.com")]
        public string Email { get; set; }
        [Required]
        public Dept? Department { get; set; }
        [Required]
        public string Job { get; set; }
        [Required]
        public string Birthday { get; set; }
        [Required]
        public string Address { get; set; }
        public IFormFile Photo { get; set; }
    }

此圖顯示了觸發 Create() 操作時的斷點。 證明值為NULL

我假設您正面臨以下錯誤:

if(model.Photo != null)
{
      string uploadsFolder = Path.Combine(hostingEnviroment.WebRootPath, "img");
      uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.Photo.FileName);
      string filePath = Path.Combine(uploadsFolder, uniqueFileName);
      model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
}

首先,請您嘗試更改您的輸入字段,如下所示:

<input asp-for="Photo" type='file' value='@Model.Photo' class="form-control custom-file-input" formenctype="multipart/form-data"/>

添加type='file' & value='@Model.Photo'

現在您應該將IFormFile值的類型發送到您的操作。

確保您的表單必須包含enctype="multipart/form-data"如下:

<form  method="post" enctype="multipart/form-data">
    <div class="form-group">
       <label asp-for="Photo"></label>
       <div class="custom-file">
           <input asp-for="Photo" class="form-control custom-file-input"/>
           <label class="custom-file-label">Choose file...</label>
       </div>
    </div>
    <input type="submit" value="Upload Image" name="submit">
</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM