文件控件未在编辑页面中初始化-剃刀页面

时间:2018-06-24 10:00:38

标签: c# razor asp.net-core razor-pages

我使用asp.net核心剃须刀页面来创建我的应用程序,在我的创建页面中,我有两个文件控件,一个用于上传图标图像,另一个用于上传详细图像。但是,当我单击“编辑”按钮时,除两个文件控件外,所有字段均已初始化。请检查我的代码。有人可以帮忙吗?

在我的剃须刀页面中:

<div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label asp-for="Product.Icon" class="control-label"></label>
                        <input asp-for="@Model.Icon" type="file" />
                        <span asp-validation-for="Product.Icon" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label”>Detail Images(support multi-uploading):</label>
                        <input type="file" id="fUpload" name="files" multiple />
                    </div>
                </div>
            </div>

在我的页面模型中:

  public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            Product = await _context.Products
                .Include(p => p.Shop).SingleOrDefaultAsync(m => m.ID == id);
            if (Product == null)
            {
                return NotFound();
            }
            ViewData["Shop"] = new SelectList(_context.Shops, "ID", "Name");
            return Page();
        }
        public async Task<IActionResult> OnPostAsync(List<IFormFile> files)
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads");
            if (!Directory.Exists(uploads))
            {
                Directory.CreateDirectory(uploads);
            }
            if (this.Icon != null)
            {
                var fileName = GetUniqueName(this.Icon.FileName);
                var filePath = Path.Combine(uploads, fileName);
                this.Icon.CopyTo(new FileStream(filePath, FileMode.Create));
                this.Product.Icon = fileName;
            }
            if (files != null && files.Count > 0)
            {
                foreach (IFormFile item in files)
                {
                    if (item.Length > 0)
                    {
                        var fn = GetUniqueName(item.FileName);
                        var fp = Path.Combine(uploads, fn);
                        item.CopyTo(new FileStream(fp, FileMode.Create));
                        this.Product.ProductImages = this.Product.ProductImages + fn + "^";
                    }
                }
            }
            _context.Attach(Product).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(Product.ID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToPage("./Index");
        }

1 个答案:

答案 0 :(得分:0)

您提供的代码<form>中缺少页面中最重要的部分。为了使文件上载正常工作,必须指定该方法为post,还必须提供一个enctype属性,其值设置为multipart/form-data

<form method="post" enctype="multipart/form-data">
...

参考:ffmpeg-srtp-documentation