ASP.NET MVC-5项目中添加PDF/DOC文件上传至数据库的实现方法
嗨,我来帮你一步步实现PDF/DOC文件上传的功能,咱们从模型、视图模型、控制器到视图依次修改,确保和你现有图片上传的逻辑保持一致:
1. 更新
PRview模型,添加报价单文件路径字段 首先需要在你的模型里新增一个字段,用来存储上传后的PDF/DOC文件路径:
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; } // 新增:存储报价单文件路径 [Display(Name = "Quote File")] public string QuoteFileUrl { get; set; } } }
2. 更新
NewPRViewModel视图模型,添加上传文件属性 视图模型需要新增一个HttpPostedFileBase类型的属性,用来接收前端上传的文件:
// 补充你的视图模型,新增报价单上传属性 public class NewPRViewModel { public PRview PRview { get; set; } public IEnumerable<PRtype> PRtypes { get; set; } public string Caption { get; set; } public HttpPostedFileBase ImageUpload { get; set; } // 新增:接收报价单上传文件 public HttpPostedFileBase QuoteFileUpload { get; set; } }
3. 修改控制器代码,处理PDF/DOC上传逻辑
接下来在Save方法里添加文件验证和保存的逻辑,和你现有图片上传的逻辑对齐:
[HttpPost] public ActionResult Save(PRview prview, NewPRViewModel model) { var validImageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; // 定义允许的文档类型:覆盖PDF、DOC、DOCX var validDocumentTypes = new string[] { "application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }; // 保留原有图片验证逻辑 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 (model.QuoteFileUpload != null && model.QuoteFileUpload.ContentLength > 0) { if (!validDocumentTypes.Contains(model.QuoteFileUpload.ContentType)) { ModelState.AddModelError("QuoteFileUpload", "Please choose a PDF, DOC or DOCX file."); } } 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; // 新增:更新数据库中的报价单文件路径 prviewInDb.QuoteFileUrl = prview.QuoteFileUrl; } // 保留原有图片保存逻辑 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; } // 新增:报价单文件保存逻辑 if (model.QuoteFileUpload != null && model.QuoteFileUpload.ContentLength > 0) { var uploadDir = "~/uploads"; // 也可以单独建"~/uploads/quotes"目录区分文件类型 var filePath = Path.Combine(Server.MapPath(uploadDir), model.QuoteFileUpload.FileName); var fileUrl = Path.Combine(uploadDir, model.QuoteFileUpload.FileName); model.QuoteFileUpload.SaveAs(filePath); prview.QuoteFileUrl = fileUrl; } _context.SaveChanges(); return RedirectToAction("Index", "PRview"); }
小提示:如果担心文件名重复覆盖,可以给文件名加上时间戳或GUID,比如
$"{Guid.NewGuid()}_{model.QuoteFileUpload.FileName}",这样能避免文件冲突。
4. 更新视图,添加上传报价单的表单元素
最后在你的视图里添加一个文件上传输入框,放在现有图片上传模块附近:
@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" }) @Html.ValidationMessageFor(m => m.ImageUpload) </div> <!-- 新增:报价单文件上传模块 --> <div> @Html.LabelFor(m => m.QuoteFileUpload, "Quote PDF/DOC") @Html.TextBoxFor(m => m.QuoteFileUpload, new { type = "file" }) @Html.ValidationMessageFor(m => m.QuoteFileUpload) </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> }
这样修改后,你就能在提交表单时上传PDF/DOC/DOCX格式的报价单文件了,文件会被保存到~/uploads目录,路径会存储到数据库的QuoteFileUrl字段中。
内容的提问来源于stack exchange,提问作者user9871793
相关产品推荐
相关产品推荐

