上传图片问题 [英] Uploading picture Problem

查看:76
本文介绍了上传图片问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys





OK快速概览



班级员工喜欢下面+一些额外的支持类

但我的问题的重点是

Hello Guys


OK Quick Overview

Class Employee like Below + some extra supporting classes
But the focus of my problem is on

public virtual byte[] EmployeePhoto {get; set;}

部分



首先,我希望将整个图像上传到数据库,然后将其检索回来查看数据库,但不要我知道为什么它看起来很难我就像我是新手这就是为什么我选择其他方式



上传文件到服务器上的文件夹和路径+文件名为一个字符串到DB不知道为什么看起来更容易。



part

First I was looking to just Upload whole image to DB and later retrieve it back to view from DB but don't know why its look hard for me like I'm new to this that's why i Chose other way

Upload File to folder on the server and path + file name as a string to DB don't know why looks easier for me.

public class Employee
    {
        [Key]
        public virtual int EmployeeID { get; set; }

        [ForeignKey("TitleID")]
        public virtual Title Title { get; set; }

        [Display(Name = "Title")]
        public virtual int TitleID { get; set; }



        [Display(Name = "First Name")]
        [Required]
        [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
        public virtual string FirstName { get; set; }
        [Display(Name = "Last Name")]
        [Required]
        [StringLength(50, ErrorMessage = "Last name cannot be longer than 50 characters.")]
        public virtual string LastName { get; set; }

        [Display(Name = "Full Name")]
        public virtual string FullName
        {
            get
            {
                return LastName + ", " + FirstName;
            }
        }

        [Display(Name = "Date of Birth")]
        [DataType(DataType.Date)]
        [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public virtual DateTime? DOB { get; set; }

        [ForeignKey("GenderID")]
        public virtual Gender Gender { get; set; }

        [Display(Name = "Gender")]
        public virtual int GenderID { get; set; }

        [Display(Name = "Adress Line 1")]
        public virtual string AddressLine1 { get; set; }
        [Display(Name = "Adress Line 2")]
        public virtual string AddressLine2 { get; set; }

        public virtual string City { get; set; }

        [Display(Name = "Post Code")]
        public virtual string PostCode { get; set; }

        [Display(Name = "Email")]
        public virtual string EmailAddress { get; set; }

        [Display(Name = "Phone Nr")]
        public virtual int Phone { get; set; }

        [Display(Name = "Employment Nr")]
        public virtual int EmploymentNr { get; set; }

        [Display(Name = "Start Date")]
        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(NullDisplayText = "" , DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public virtual DateTime? HireDate { get; set; }

        [Display(Name = "Holiday Total Allowed")]
        public virtual int HolidayTotal { get; set; }

        [Display(Name = "Sick Leave Total Allowed")]
        public virtual int SickLeaveTotal { get; set; }

        [ForeignKey("ShiftID")]
        public virtual Shift Shift { get; set; }

        [Required]
        [Display(Name = "Shift")]
        public virtual int ShiftID { get; set; }

        public virtual byte[] EmployeePhoto { get; set; }



    }

    public class Title
    {
        public virtual int TitleID { get; set; }
        [Column("Title")]
        [Display(Name = "Title")]
        [Required]
        public virtual string Name { get; set; }

    }
    public class Gender
    {
        public virtual int GenderID { get; set; }
        [Column("Gender")]
        [Display(Name = "Gender")]
        [Required]
        public virtual string Name { get; set; }
    }
    public class Shift
    {
        public virtual int ShiftID { get; set; }
        [Column("Shift")]
        [Display(Name = "Shift")]
        [Required]
        public virtual string Name { get; set; }
        [Display(Name = "Start Time")]
        [Required]
        [DataType(DataType.Time)]
        [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:hh:mm}", ApplyFormatInEditMode = true)]
        public virtual DateTime StartTime { get; set; }
        [Display(Name = "End Time")]
        [Required]
        [DataType(DataType.Time)]
        [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:hh:mm}", ApplyFormatInEditMode = true)]
        public virtual DateTime EndTime { get; set; }


    }
}







我想我需要改变




I think I need to change

public virtual byte[] EmployeePhoto {get; set;}



to


to

public virtual string Photo {get; set;}





然后编辑创建员工的视图模型,参与Model.Photo



并添加



Then Edit the View Model for Create Employee, take part of Model.Photo

And add

<form action="Categories/Upload" enctype="multipart/form-data" method="post">
    <input type="file" name="Image">
    <input type="submit" value"Save">
</form>







然后我需要编辑我的控制器添加





类似






Then I need to edit my Controller Adding


Something like

[HttpPost]
        public ActionResult Upload(string ActionName)
        {
            var path = Server.MapPath("~/App_Data/Files");
            foreach (string item in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[item];
                if (file.ContentLength == 0)
                {
                    //Repeated upload file be skipped .
                    continue;
                }
                string savedFileName = Path.Combine(path, Path.GetFileName(file.FileName));
                file.SaveAs(savedFileName);
            }
            return RedirectToAction(ActionName);
        }







但我不知道如何保存文件路径作为我的数据库照片栏的字符串



这是问题,这是我需要一些帮助的问题





我也有一个服装Html帮手以便以后检索整件事情








but I don't Know how to Perform saving of the file path as a string to my DB Photo Column

that's the issue and that's the problem i need some help


also i have a costume Html helper to retrieve the whole thing later from


public static class CustomHtmlHelper
    {
        public static IHtmlString Image(this HtmlHelper helper, string src, string alt)
        {
            // Build <img> tag
            TagBuilder tb = new TagBuilder("img");
            // Add "src" attribute
            tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
            // Add "alt" attribute
            tb.Attributes.Add("alt", alt);
            // return MvcHtmlString. This class implements IHtmlString
            // interface. IHtmlStrings will not be html encoded.
            return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
        }
    }





结果将是





result will be

@Html.Image(Model.Photo, Model.AlternateText)





什么会在我的照片中返回detiles视图模型





我正在学习其他解决方案,这可能是一种错误的方法




最好的问候



What will return Photo in my detiles view model


I'm also open for other solutions as I'm learning and this may be a wrong approach


Best Regards

推荐答案

您有保存图像的代码。你是对的,你需要将图像属性更改为字符串,如果你要保存图像的路径。我还建议像这样的文件夹结构:



图片/用户ID / img文件



这样两个用户可以上传具有相同名称的图像,但不会发生冲突,因为每个用户都有一个以标识用户的主键命名的文件夹。
You have the code to save your image. You are right, you need to change the image property to a string, if you're going to save a path to the image. I also suggest a folder structure like this:

pictures/userid/img file

so that two users can upload images with the same name, but they won't clash, as each user has a folder that is named after the primary key that identifies the user.


这篇关于上传图片问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆