如何上传和保存多个图像到数据库

时间:2014-10-20 12:46:54

标签: asp.net-mvc-4 c#-4.0 visual-studio-2012 razorengine

我有这个动作方法:

[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);            
        }

这是我的观点:

<div id="tabs-3">

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

    <div class="form-field">
        <p>Select pictures:</p>
        <div class="upload-container">
            <div class="upload">
                <input type="file" name="files" 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> 

我想将更多图像存储到数据库中。用户可以选择一个或多个图像,上传后必须将图像保存到数据库中。

但在这一行:

 user.Image = new byte[files.ContentLength];

无法识别contentLength。

谢谢

这是我的模特:

public partial class UserProfile
    {

        public int Id { get; set; }
        public string UserName { get; set; }       
         [Display(Name = "First Name")]
        public string FirstName { get; set; }       
          [Display(Name = "Last Name")]
        public string LastName { get; set; }
        public string Email { get; set; }      
          [Display(Name = "Motto")]
        public string Motto { get; set; }

          [Display(Name = "Place of birth")]
        public string PlaceOfBirth { get; set; }
          [Display(Name = "How many bikes")]
        public int    HowManyBikes { get; set; }
          [Display(Name = "Are they beside your bed")]
        public string BesideYourBeth { get; set; }
          [Display(Name = "Nicest ride")]
        public string NicestRide { get; set; }
          [Display(Name = "Worst ride")]
        public string WorstRide { get; set; }
          [Display(Name = "Amount of k's per year")]
        public string AmountKmPerYear { get; set; }
          [Display(Name = "Average speed")]
        public string AverageSpeed { get; set; }
          [Display(Name = "Able to chat while riding")]
        public string AbleToChatWhileRiding { get; set; }
          [Display(Name = "Phone number")]
        public string PhoneNumber { get; set; }
        public string Picture { get; set; }

        [Column(TypeName = "image")]


        [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; }


    }

如果我选择多个图像,例如两个,我只在地图中看到一张照片:\ App_Data \ Uploads。

因为用户的个人资料中可以有一张或多张照片。

我添加了这个:

if (user != null)
                    {
                        var lolaphoto = new LolaBikePhoto
                        {
                            UserProfileID = user.Id
                        };

                       // lolaphoto.UserProfileID = 47;
                        db.lolabikerPhotos.Add(lolaphoto);
                    }

0 个答案:

没有答案