【问题标题】:System can't read my Image upload file (?)系统无法读取我的图片上传文件 (?)
【发布时间】:2018-07-20 12:42:44
【问题描述】:

做电子商务网站并解决图片上传问题。基本上我所有的产品信息都传递到数据库,除了图像文件。好像读不出来这是我的方法:

    [HttpPost]
    [Route("/create_product")]
    public async Task<IActionResult> productAdd(ProductCheck check)
    {
        int? id = HttpContext.Session.GetInt32("userId");
        if(id == null)
        {
            return RedirectToAction("LoginPage", "User");                           
        }
        else
        {
            if(ModelState.IsValid)
            {
                User exists = _context.Users.Where(u=>u.UserId == id).SingleOrDefault();
                Product newProduct = new Product
                {
                    Title = check.Title,
                    Description = check.Description,
                    Price = check.Price,  
                    UserId = (int)id,
                    CreatedAt = DateTime.Now,
                };
                var uploadDestination = Path.Combine(_hostingEnvironment.WebRootPath, "uploaded_images");

                if (check.Image != null)
                {
                    var filepath = Path.Combine(uploadDestination, check.Image.FileName);
                    using (var fileStream = new FileStream(filepath, FileMode.Create))
                    {
                        await check.Image.CopyToAsync(fileStream);
                        newProduct.Picture = "/uploaded_images/" + check.Image.FileName;
                    }
                }
                _context.Add(newProduct);
                _context.SaveChanges();
            }
            else
            {
                TempData["error"] = "Not added. Error";
            }
            return RedirectToAction("Homepage");

我之前遇到的错误是:Image

这是我的模型:

using System;

使用 System.Collections.Generic;

命名空间 sellwalker.Models { 公共类产品:BaseEntity {

    public int ProductId{get;set;}
    public string Title{get;set;}
    public string Description{get;set;}
    public decimal Price{get;set;}
    public string Picture { get; set; }
    public DateTime CreatedAt{get;set;}
    public List<Order> Orders {get; set;}
    public Product()
    {
        Orders = new List<Order>();
    }
    public int UserId{get;set;}
    public User Seller{get;set;} 
}

和 ViewModel 验证表:

public class ProductCheck : BaseEntity
{
    [Required]
    [MinLength(2, ErrorMessage="Name of the product should be greater than 2 characters")]
    public string Title{get;set;}

    [Required]
    [MinLength(10, ErrorMessage="Description of the product should be greater than 10 characters")]
    public string Description{get;set;}

    [Required]
    public decimal Price{get;set;}

    public IFormFile Image { get; set; }
}

【问题讨论】:

  • 你是通过ajax发送信息吗?
  • 你能把视图的代码贴出来吗?

标签: c# asp.net .net postgresql entity-framework


【解决方案1】:

图!确保您的表单中有 enctype="multipart/form-data"!

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2019-06-22
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多