简体   繁体   English

如何使用 ASP.NET Core 5.0 MVC 上传图片

[英]How to upload an image with ASP.NET Core 5.0 MVC

I want to upload an image in a post method for edit a record,this is the form that send the data to the controller:我想在 post 方法中上传图像以编辑记录,这是将数据发送到控制器的表单:

<form asp-action="Edit">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <input type="hidden" asp-for="IdEmpresa" />
    <div class="form-group mt-3">
        <label asp-for="CodEmpresa" class="control-label"></label>
        <input required asp-for="CodEmpresa" class="form-control" />
        <span asp-validation-for="CodEmpresa" class="text-danger"></span>
    </div>
    <div class="form-group mt-3">
        <label asp-for="Empresa" class="control-label"></label>
        <input required asp-for="Empresa" class="form-control" />
        <span asp-validation-for="Empresa" class="text-danger"></span>
    </div>
    <div class="form-group mt-3">
        <label asp-for="Contacto" class="control-label"></label>
        <input required asp-for="Contacto" class="form-control" />
        <span asp-validation-for="Contacto" class="text-danger"></span>
    </div>
    <div class="form-group mt-3">
        <label asp-for="Imagen" class="control-label"></label>
        <input asp-for="Imagen" class="form-control" type="file" />
        <span asp-validation-for="Imagen" class="text-danger"></span>
    </div>
    <br />
    <div class="form-group  d-flex justify-content-end">
        <input type="submit" value="Actualizar" class="btn btn-primary bg-primary" />
    </div>
</form>

And here it's the method that update the data but only the strings or the numbers, i don't how to handle the input of type = 'file' to copy the image to a local directory and assign the path to the string of 'Image' attribute in the model这里是更新数据但只更新字符串或数字的方法,我不知道如何处理 type = 'file' 的输入以将图像复制到本地目录并将路径分配给'Image ' 模型中的属性

public async Task<IActionResult> Edit(int id, [Bind("IdEmpresa,CodEmpresa,Empresa,Contacto")] MnEmpresa mnEmpresa)
{
    if (id != mnEmpresa.IdEmpresa)
    {
        return NotFound();
    }

    if (ModelState.IsValid)
    {
        try
        {
            MnEmpresa empresa = _context.MnEmpresas.Where(x => x.IdEmpresa == mnEmpresa.IdEmpresa).FirstOrDefault();
            _context.Entry(empresa).State = EntityState.Detached;
            mnEmpresa.Imagen = empresa.Imagen;
            _context.Update(mnEmpresa);
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!MnEmpresaExists(mnEmpresa.IdEmpresa))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return RedirectToAction(nameof(Index));
    }

    return View(mnEmpresa);
}

Here is my model:这是我的模型:

public MnEmpresa()
{
    MnAplicactivos = new HashSet<MnAplicactivo>();
    MnUsuarios = new HashSet<MnUsuario>();
}

public int IdEmpresa { get; set; }
public int? CodEmpresa { get; set; }
public string Empresa { get; set; }
public string Contacto { get; set; }

public string Imagen { get; set; }

public virtual ICollection<MnAplicactivo> MnAplicactivos { get; set; }
public virtual ICollection<MnUsuario> MnUsuarios { get; set; }

To upload a file use enctype="multipart/form-data" attribute in the form tag.要上传文件,请在form标签中使用enctype="multipart/form-data"属性。 The enctype attribute can be used only if method="post".仅当 method="post" 时才能使用enctype属性。

<form asp-action="Edit" method="post" enctype="multipart/form-data">
    ....
    <input type="file" name="postedFile" id="postedFile" multiple />

    <div class="form-group  d-flex justify-content-end">
        <input type="submit" value="Actualizar" class="btn btn-primary bg-primary" />
    </div>
<form>

In the the controller:在控制器中:

public class YourController : Controller
{
    private readonly ILogger<HomeController> _logger;
    private readonly IWebHostEnvironment _web;

    public YourController(IWebHostEnvironment env, ILogger<HomeController> logger)
    {
        _web = env;
        _logger = logger;
    }

    ....

    [HttpPost]
    public async Task<IActionResult> Edit(int id, [Bind("IdEmpresa,CodEmpresa,Empresa,Contacto")] MnEmpresa mnEmpresa, IFormFile postedFile)
    { 
        if (id != mnEmpresa.IdEmpresa)
        {
            return NotFound();
        }
    
        if (ModelState.IsValid)
        {         
            string wwwPath = _web.WebRootPath;
            string contentPath = _web.ContentRootPath;
    
            string path = Path.Combine(_web.WebRootPath, "Uploads");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }          
    
            if (postedFile != null)
            {         
                var target = Path.Combine(path, fileName);     
                string fileName = Path.GetFileName(postedFile.FileName);
                using (FileStream stream = new FileStream(target, fileName), FileMode.Create))
                {                
                    postedFile.CopyTo(stream);
                }
    
                #region Your previous code
                try
                {
                    MnEmpresa empresa = _context.MnEmpresas.Where(x => x.IdEmpresa == mnEmpresa.IdEmpresa).FirstOrDefault();
                    _context.Entry(empresa).State = EntityState.Detached;
                    mnEmpresa.Imagen = target;
                    _context.Update(mnEmpresa);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!MnEmpresaExists(mnEmpresa.IdEmpresa))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }    
                return RedirectToAction(nameof(Index));
                #endregion
            }
            return View();
        }
        return View(mnEmpresa);    
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM