Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Samuel Watts 4 posts 74 karma points
    Apr 13, 2016 @ 22:34
    Samuel Watts
    0

    How to upload image file alongside text with SQL server 2008

    Hi all, fairly new to Umbraco and the MVC Framework. Been working on a upload form today and cannot for the life of me seem to get the image upload to work.

    My Model:

    using System;
    

    using System.Collections.Generic; using System.Linq; using System.Web; using Umbraco.Core.Persistence;

    namespace UmbracoSite.Models {

    [TableName("UmbracoBadge")]
    [PrimaryKey("badge_id", autoIncrement = true)]
    public class Badges
    {
        public int badge_id { get; set; }
        public string badge_name { get; set; }
        public string badge_image { get; set; }
        public string badge_desc { get; set; }
        public DateTime added_date { get; set; }
    
        public string BadgeImages
        {
           get { return badge_image.Replace(" ", string.Empty) + ".jpg"; }
        }
    }
    

    }

    My Controller:

    using System;
    

    using System.IO; using System.Web; using System.Web.Mvc; using Umbraco.Web.Mvc; using UmbracoSite.Models; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net;

    namespace UmbracoSite.Controllers { public class BadgeSurfaceController : SurfaceController {

        //[HttpPost]
        //public ActionResult AddBadge(Badges badge, HttpPostedFile file)
        //{
    
        //    badge.added_date = DateTime.Now;
    
        //    string ImageName = badge.badge_image;
        //    string physicalPath = Server.MapPath("~/images/BadgeImages/" + ImageName);
    
        //    file.SaveAs(physicalPath);
    
        //    var repo = new BadgeRepository();
    
        //    repo.Insert(badge);
    
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        file.InputStream.CopyTo(ms);
        //        byte[] array = ms.GetBuffer();
        //    }
        //    return CurrentUmbracoPage();
        //}
    
    
        [HttpPost]
        public ActionResult UploadPhoto(string badgeimage, HttpPostedFileBase photo)
        {
            string path = "~/images/BadgeImages/" + badgeimage;
    
            if (photo != null)
                photo.SaveAs(path);
    
            return CurrentUmbracoPage();
        }
    
    
    
    }
    

    }

    My view:

    @model UmbracoSite.Models.Badges
    

    @using (Html.BeginUmbracoForm("AddBadge", "BadgeSurface", null, new { @class = "badge-form" })) {

        <h1>Badge Creator</h1>
    
        <div id="badgeFormInput">
            <p>@Html.TextBoxFor(x => x.badge_name, new { @class = "badgeName", @id = "txtBadgeName", @placeholder = "Badge Name", autocomplete = "off" })</p>
            <p>@Html.TextAreaFor(x => x.badge_desc, new { @class = "badgeDesc", @id = "txtBadgeDesc", @placeholder = "Badge Description", autocomplete = "off" })</p>
            <form action="" enctype="multipart/form-data" method="post"><label>Upload Badge Image:</label>
                <input class="uploadButton" id="photo" type="file" name="photo">
                <input class="uploadButton" type="submit" value="Upload"/>
                <input type="hidden" name="badgeImage" value="@Html.DisplayFor(modelItem => Model.BadgeImages)"/>
            </form>
            <p><input class="homelogbtn" type="submit" value="Submit" /></p>
        </div>
    </div>
    

    }

    I'm also using a repository for the Sql :

    using System;
    

    using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Umbraco.Core; using Umbraco.Core.Persistence; using UmbracoSite.Models;

    namespace UmbracoSite { public class BadgeRepository { private readonly UmbracoDatabase _database;

        public BadgeRepository()
        {
            _database = ApplicationContext.Current.DatabaseContext.Database;
        }
    
        public IList<Badges> Getall()
        {
            return _database.Fetch<Badges>("SELECT * FROM UmbracoBadge");
        }
    
        //[HttpPost]
        //public ActionResult UploadPhoto(string badgeImages, HttpPostedFileBase photo)
        //{
        //    string path = "~/images/BadgeImages/" + badgeImages;
    
        //    if (photo != null) photo.SaveAs(path);
    
        //    return CurrentUmbracoPage();
        //}
    
        public void Insert(Badges badge)
        {
            _database.Insert(badge);
        }
    }
    

    }

    Any help would be greatly appreciated!!

    Thanks, Sam

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies