将pdf或doc上传到ASP.NET MVC-5应用程序的数据库中

时间:2018-05-30 17:56:50

标签: sql-server asp.net-mvc

嗨, 我一直在学习ASP.NET以及通过MVC应用程序完成一个小项目。 我的申请是基于机构提出产品的购买请求。它是所有数据存储在DB中的基本形式。 如何将上传PDF或DOC添加到此编码中,以便在提交表单时上传pdf报价。

**

  1. 我的模特

  2. **

    namespace sparki02.Models{
    public class PRview
    {
        public int Id { get; set; }
        [DataType(DataType.Html)]
        public string Caption { get; set; }
        [Required]
        [DataType(DataType.ImageUrl)]
        public string ImageUrl { get; set; }
        [Required]
        [StringLength(100)]
        [Display(Name = "Supplier Name")]
        public string SupplierName { get; set; }
        [Required]
        [StringLength(255)]
        [Display(Name = "Material Name")]
        public string MATName { get; set; }
        [Required]
        [StringLength(100)]
        public string Brand { get; set; }
        public PRtype PRtype { get; set; }
        [Display(Name = "PR Type")]
        public byte PRtypeId { get; set; }
        [Required]
        [Display(Name = "Unit Price")]
        public double Unitprice { get; set; }}}
    

    **

    2。控制器

    **

        public ActionResult NewPR()
        {var prtypes = _context.PRtypes.ToList();
         var viewModel = new NewPRViewModel
        {PRview = new PRview(), 
         PRtypes = prtypes};
    
        return View("NewPR", viewModel);
        }
    
        [HttpPost]
        public ActionResult Save(PRview prview, NewPRViewModel model)
        {
    
            var validImageTypes = new string[]
            {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/png"
            };
    
            if (model.ImageUpload == null || model.ImageUpload.ContentLength == 0)
            {
                ModelState.AddModelError("ImageUpload", "This field is required");
            }
            else if (validImageTypes.Contains(model.ImageUpload.ContentType))
            {
                ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image.");
            }
    
    
    
            if (!ModelState.IsValid)
            {
                var viewModel = new NewPRViewModel
                {
                    PRview = prview, 
                    PRtypes = _context.PRtypes.ToList(),
                    Caption = model.Caption
                };
    
    
                return View("NewPR", viewModel);
            }
    
            if (prview.Id == 0)
                _context.PRviews.Add(prview);
    
            else
            {
    
                var prviewInDb = _context.PRviews.Single(c => c.Id == prview.Id);
    
                prviewInDb.PRtypeId = prview.PRtypeId; 
                prviewInDb.MATName = prview.MATName;
                prviewInDb.SupplierName = prview.SupplierName;
                prviewInDb.Brand = prview.Brand;
                prviewInDb.Unitprice = prview.Unitprice;
                prviewInDb.ImageUrl = prview.ImageUrl;
    
                if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
                {
                    var uploadDir = "~/uploads";
                    var imagePath = Path.Combine(Server.MapPath(uploadDir), model.ImageUpload.FileName);
                    var imageUrl = Path.Combine(uploadDir, model.ImageUpload.FileName);
                    model.ImageUpload.SaveAs(imagePath);
                    prview.ImageUrl = imageUrl;
    
                }
    
            }
    
            _context.SaveChanges();
            return RedirectToAction("Index", "PRview");
    

    **

    1. 我的视图块

    2. **仅与文件上传相关的内容

      { @model sparki02.ViewModels.NewPRViewModel
        @using (Html.BeginForm("Save", "PRview", FormMethod.Post, new { enctype =    "multipart/form-data" }))
      {@Html.ValidationSummary(true, "Please fix the following errors.")
      <div>
          @Html.LabelFor(m => m.Caption)
          @Html.EditorFor(m => m.Caption)
      </div>
      <div>
          @Html.LabelFor(m => m.ImageUpload)
          @Html.TextBoxFor(m => m.ImageUpload, new { type = "file" })
      </div>
      <button type="submit">Create</button>
      
          <div class="form-group">
              <button type="submit" class="btn btn-primary">Save</button>
          </div>
          <div class="form-group"> <p> @DateTime.Now</p> </div>
      

      }

0 个答案:

没有答案