Hi, I have been learning ASP.NET as well as doing a small project through MVC application. My application is based on raising a purchase request for a product by an institution. It's a basic form through which all data are stored in DB. How to add upload PDF or DOC into this coding so that the pdf quotation can be uploaded while submitting the form.
**
My Model
**
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. Controller
**
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");
**
My View block
** Only content related to file upload
{ @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>
}