【问题标题】:Muliple images from database来自数据库的多个图像
【发布时间】:2014-10-23 09:23:29
【问题描述】:

我想上传多张图片并将它们保存到数据库中。但在控制器操作中:UploadMorePhotos。我在文件中看到我选择的文件,但如果通过这一行:file.SaveAs(path); 它只保存一个图像,然后转到if (ModelState.IsValid)。那么如何上传您选择的所有图像。谢谢你

我有这个:

控制器动作:

       [HttpPost]
        public ActionResult UploadMorePhotos(UserProfile userProfile, IEnumerable<HttpPostedFileBase>files)
        {
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
                    //ModelState.Clear();
                }


                if (ModelState.IsValid)
                {
                    string username = User.Identity.Name;
                    // Get the userprofile
                    UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));

                    // Update fields
                    user.Image = new byte[file.ContentLength];
                    file.InputStream.Read(user.Image, 0, file.ContentLength);
                    user.ImageMimeType = file.ContentType;

                    db.Entry(user).State = EntityState.Modified;

                    db.SaveChanges();

                }

                    return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
            }

            return View(userProfile);            
        }


 public ActionResult GetImage(int id)
        { 
            byte[] image = db.userProfiles.Where(p => p.Id == id).Select(img => img.Image).FirstOrDefault();
            //var v = // FirstOrDefault(u => u.Id.Equals(itemId));
           // image = v;
            return File(image,"image/jpeg");            
        }

型号:

    [DisplayName("Image")]
            [MaxLength]

            public byte[] Image { get; set; }

            [Display(Name = "Display profile Image")]
            public bool DisplayItem { get; set; }

              [DisplayName("ImageMimeType")]
            public string ImageMimeType { get; set; }

              public virtual ICollection<LolaBikePhoto> LolaBikePhotos { get; set; }

public class LolaBikePhoto
    {
        [Key]
        public int PhotoID { get; set; }
        public int UserProfileID { get; set; }
        public string Photo { get; set; }
        public string Notes { get; set; }

        public virtual UserProfile userProfile { get; set; }

    }

和视图:

<div id="tabs-3">


           @using (Html.BeginForm("UploadMorePhotos", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
           {
               @Html.AntiForgeryToken()


            <div class="form-field">
                <p>Select pictures:</p>
                <div class="upload-container">
                    <div class="upload">
                        <input type="file" name="files" value="" multiple="multiple" id="file1" />
                        <img src="@Url.Content("~/images/deleteButton.png")" alt="Remove picture." />
                    </div>
                </div>
            </div>

            <div class="form-field">
                <input type="submit" value="Create" />
            </div>
           }
        </div>

我现在是这样的:

[HttpPost]
        public ActionResult UploadMorePhotos(UserProfile userProfile, IEnumerable<HttpPostedFileBase>files)
        {
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
                    //ModelState.Clear();
                }


                if (ModelState.IsValid)
                {
                    string username = User.Identity.Name;
                    // Get the userprofile
                    UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));

                    // Update fields
                    user.Image = new byte[file.ContentLength];
                    file.InputStream.Read(user.Image, 0, file.ContentLength);
                    user.ImageMimeType = file.ContentType;

                    db.Entry(user).State = EntityState.Modified;

                    db.SaveChanges();

                }

                    //return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
            }

            return View(userProfile);            
        }

但仍然只有一张图片

返回视图(用户配置文件); }

        return View(userProfile);            
    }

【问题讨论】:

    标签: c# jquery-ui asp.net-mvc-4 visual-studio-2012 entity-framework-4


    【解决方案1】:

    让我们从循环中减少脂肪:

    foreach (var file in files)
    {
        if (file.ContentLength > 0)
        {
            file.SaveAs(path);
        }
    
    
        if (ModelState.IsValid)
        {
            db.SaveChanges();
        }
        return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
    }
    

    您将在第一次迭代结束时从循环中返回。

    如果你删除

        return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
    

    你的循环应该完成了。

    更新

    但我想在上传图片后留在 tabs-3 上

    然后替换

    return View(userProfile);
    

    return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
    

    确保在循环完成后返回

    【讨论】:

    • 但我想在上传图片后留在 tabs-3
    • 那么不要删除它,而是用它替换return View(userProfile)。只要命中任何return 语句,您的方法就会返回。
    • 那么最后的回报是什么?
    • 返回视图(userProfile); } 返回视图(用户配置文件); }
    • 两次userProfile?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2016-01-08
    • 1970-01-01
    • 2011-09-04
    • 2016-11-02
    • 1970-01-01
    相关资源
    最近更新 更多