繁体   English   中英

Asp.net Mvc 6组合浏览图像并将图像大小图像名称添加到sql server中

[英]Asp.net Mvc 6 Combine browse image and add image size image name into sql server

我看到了此链接http://forums.asp.net/t/1976497.aspx?ASP%20Net%20MVC%205%20Upload%20Image%20Save%20to%20Database%20Create%20Thumbnail%20and%20Display%20in%20View ,这是一个很好的示例,但是它不适用于Asp.Net Mvc 6,因为Mvc 6中不存在HttpPostedFileBase。有人为我提供了Mvc 6的此代码吗?

//控制器代码

public IActionResult Create(PublisherInfos publisherInfos, IFormFile file)
    {
        if (ModelState.IsValid)
        {
            //attach the uploaded image to the object before saving to Database
            file = Request.Form.Files.GetFile("CoverImage");

            //get posted file from web form
            var filename = publisherInfos.FileName;
            var filePathOriginal = _appEnv.ApplicationBasePath + "\\wwwroot\\images";
            string savedFileName = Path.Combine(filePathOriginal, filename);
            file.SaveAs(savedFileName);

            //initialize file upload class to save file to wwwroot directory
            FormUpload upload = new FormUpload();

            //get the uploaded file name
            string Photo = upload.SaveFile(file);
            if (Photo != "")
            {
                Photo = Url.Content($"~/upload/{Photo}");
            }
            // Add file size and file name into Database
            _context.PublisherInfos.Add(publisherInfos);
            _context.SaveChanges();
            return RedirectToAction("Index", new { Message = PublisherInfoMessageId.DataloadSuccess });

        }           

        return View(publisherInfos);
    }

//然后,类模型如下:// FormUpload类

public class FormUpload
{
    private static string UploadDestination { get; set; }
    private static string[] AllowedExtensions { get; set; }

    IApplicationEnvironment _appEnv;

    public FormUpload()
    {

        //upload config
        FormUpload.AllowedExtensions = new string[] { ".jpg", ".png", ".gif" };
        FormUpload.UploadDestination = _appEnv.ApplicationBasePath + "\\wwwroot\\images";
    }
    private bool VerifyFileExtension(string path)
    {
        return FormUpload.AllowedExtensions.Contains(Path.GetExtension(path));
    }
    private bool VerifyFileSize(IFormFile file)
    {
        Double fileSize = 0;
        using (var reader = file.OpenReadStream())
        {
            //get filesize in kb
            fileSize = (reader.Length / 1024);
        }
        //filesize less than 1MB => true, else => false
        return (fileSize < 1024) ? true : false;
    }
    public string SaveFile(IFormFile file)
    {
        string Filename = "";
        if (file.ContentDisposition != null)
        {
            //parse uploaded file
            var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
            Filename = parsedContentDisposition.FileName.Trim('"');
            string uploadPath = FormUpload.UploadDestination + Filename;

            //check extension
            bool extension = this.VerifyFileExtension(uploadPath);
            if (extension == false)
            {
                return "";
            }
            //check file size
            bool filesize = this.VerifyFileSize(file);
            if (filesize == false)
            {
                return "";
            }
            //save the file to upload destination
            file.SaveAs(uploadPath);
        }
        return Filename;
    }
}

// PublisherInfos模型

public class PublisherInfos
{
    [Key]
    [Required]
    public int PubInfoId { get; set; }

    [Column(TypeName = "int")]
    [Display(Name = "Image Size")]
    public int ImageSize { get; set; }

    [StringLength(int.MaxValue, MinimumLength = 8)]
    [Column(TypeName = "nvarchar(Max)")]
    [DataType(DataType.Text)]
    [Display(Name = "Image Filename")]
    public string FileName { get; set; }

    [Column(TypeName = "Image")]
    [Display(Name = "Book Cover")]
    public byte[] CoverImage { get; set; }

}

//创建发布者信息表单

 <form asp-action="Create"> <div class="form-horizontal"> <h4>PublisherInfos</h4> <hr /> <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="PubId" class="col-md-2 control-label"></label> <div class="col-md-10"> <select asp-for="PubId" asp-items="@ViewBag.Publishers" class="form-control"></select> </div> </div> <div class="form-group"> <label asp-for="CoverImage" class="col-md-2 control-label"></label> <div class="col-md-10"> <input name="CoverImage" class="form-control" /> <input type="file" class="" /> </div> </div> <div class="form-group"> <label asp-for="ImageSize" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="ImageSize" class="form-control" /> <span asp-validation-for="ImageSize" class="text-danger" /> </div> </div> <div class="form-group"> <label asp-for="FileName" class="col-md-2 control-label"></label> <div class="col-md-10"> <input asp-for="FileName" class="form-control" /> <span asp-validation-for="FileName" class="text-danger" /> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-primary" /> </div> </div> </div> </form> 

// ----------------------------------------上面的代码,我为发布者信息表,用于将图像存储在CoverImage字段中,将文件大小添加到ImageSize字段中,并将文件名存储在FileName字段中。 但是我无法获得要存储在Asp.Net Mvc 6中的数据库中的结果。1.此代码有什么问题? 2.您能帮我发现错误吗? 谢谢,

在HttpPostedFileBase的位置使用“ IFormFile”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM