【问题标题】:upload an image with mvc5使用 mvc5 上传图片
【发布时间】:2014-09-30 17:31:16
【问题描述】:

我无法将图像从网站上传到服务器。

我的 ajax 表单正在获取除图像文件之外的所有内容。我在这里看到了类似的问题ASP.Net MVC 5 image upload to folder

但我似乎无法让它工作

我有这个

public HttpPostFileBase imageFile {get;set;}
public string imageText {get;set;}
public string imageTitle {get;set;}
public bool isActive {get;set;}
public DateTime dateAdded {get;set;}
public string urlRedirect {get;set;}

public ActionView Index()
{
   return View;
}

public void UploadImage (CarouselController carouselImage)
{
 // some code
}

我的 ajax begin 表单包含所有带有 html razor 的字段。

@Html.TextBoxFor(model =>model.imageText , new {class = "form-control"} )
... similar for the other fields. 
for the image i have 

@html.TextBoxFor(model => model.imageFile), new { type="file"})

这使它成为一个有效的选择输入框,但它不会将任何信息传递给上传方法。

我的意思见附图。

【问题讨论】:

  • imageFile 是实际文件还是图片所在的位置?
  • 我不确定。它是 的等价物,那是图像还是位置?但无论哪种方式,我都认为它没有通过,因为我尝试将 HttpPostFileBase 转换为字符串、对象、HttpPostFile、HttpPostFileBase,并且绝望中我也尝试了一个 byte[]

标签: c# ajax asp.net-mvc razor


【解决方案1】:

您需要在表单上设置以下属性:

enctype = "multipart/form-data"

【讨论】:

  • 我会试一试的。直到稍后我才能对其进行测试。
  • 不,不幸的是,这仍然不起作用,它继续填充 null。
  • 而且我还假设您的屏幕截图已经过时并且您的 HttpPostFileBase 被称为“文件”,即使屏幕截图称它为“imageFile”?
  • 截图是实际代码。另一个来自记忆。但我只是检查了一下,除了名字。代码几乎就在
  • 在这种情况下,我会仔细检查您在这些属性上的名称是否正确。如果您尝试发布名为“file”的内容,服务器只会将其绑定到名为“file”的内容
【解决方案2】:

您似乎正在尝试发送文件以及使用 AJAX 发布的表单。如果是这种情况,那么这就是您的问题:您不能以这种方式发送文件。 AJAX 表单发布仅支持纯文本值。如需解决方法,请参阅我对this 问题的回答。

如果您需要任何说明,请告诉我。

【讨论】:

    【解决方案3】:
    public class ImageUpload
    {
            public int ID { get; set; }       
    
            public string Title { get; set; }
    
            public string Description { get; set; }       
    
            [AllowHtml]       
            public string Contents { get; set; }
    
            public byte[] Image { get; set; }
    }
    
    public ActionResult Create()
    {
         return View();
    }
    
    @using (Html.BeginForm("Create","Content", FormMethod.Post, new { enctype ="multipart/form-data" }))
    { 
       <input type="file" name="ImageData" id="ImageData" onchange="fileCheck(this);" />
    }
    
    [Route("Create")]
    [HttpPost]
    public ActionResult Create(ImageUpload model)
    {
        HttpPostedFileBase file = Request.Files["ImageData"];
    
        ContentRepository service = new ContentRepository();
    
        int i = service.UploadImageInDataBase(file, model);
    
        if (i == 1)
        {
             return RedirectToAction("Index");
        }
    
        return View(model);
    }
    
    private readonly ImageDBContext db = new ImageDBContext();
    
    public int UploadImageInDataBase(HttpPostedFileBase file, ImageUplad imageupload)
    {
            ImageUpload.Image = ConvertToBytes(file);
            var Content = new Content
            {
                Title = imageupload.Title,
                Description = imageupload.Description,
                Contents = imageupload.Contents,
                Image = imageupload.Image
            };
    
            db.Contents.Add(Content);
    
            int i = db.SaveChanges();
    
            if (i == 1)
            {
                return 1;
            }
            else
            {
                return 0;
            }
    }
    
    public byte[] ConvertToBytes(HttpPostedFileBase image)
    {
        byte[] imageBytes = null;
    
        BinaryReader reader = new BinaryReader(image.InputStream);
    
        imageBytes = reader.ReadBytes((int)image.ContentLength);
    
        return imageBytes;
    }
    
    Step 6: Display an image form database on view. Here we display the content and image from the database.
    
    public ActionResult Index()
    {
        var content = db.Contents.Select(s => new
        {
            s.ID,
            s.Title,
            s.Image,
            s.Contents,    
            s.Description    
          });
    
          List<ImageUpload> imageModel = content.Select(item => new ImageUpload()    
          {    
                ID = item.ID,    
                Title = item.Title,    
                Image = item.Image,    
                Description = item.Description,    
                Contents = item.Contents    
            }).ToList();
    
           return View(imageModel);    
     }
    
    <td>
         <img src="/Content/RetrieveImage/@item.ID" alt="" height=100 width=200 />
    </td>
    
    public ActionResult RetrieveImage(int id)
    {
        byte[] cover = GetImageFromDataBase(id);
    
        if (cover != null)
        {
            return File(cover, "image/jpg");
        }
        else
        {
            return null;
        }
    }
    
    public byte[] GetImageFromDataBase(int Id)
    {
        var q = from temp in db.Contents where temp.ID == Id select temp.Image;
    
        byte[] cover = q.First();
    
        return cover;
    }
    

    // 这是一个如何上传图像并返回其视图的示例。更多信息请参考https://www.c-sharpcorner.com/UploadFile/b696c4/how-to-upload-and-display-image-in-mvc/

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 2017-02-21
      • 2021-03-19
      • 2015-12-25
      • 2015-07-11
      • 2013-11-03
      • 2017-08-03
      • 2015-02-10
      相关资源
      最近更新 更多