I am trying to validate file upload for mime type and other validations, i am uploading three different type of formats, pdf, zip and ppt, so please tell me how can i check whether the file is a valid pdf not any text file with just extension change.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace OnlineStationaryRegister.Models
{
public class TourDetail
{
[Key]
public int FileId { get; set; }
public string Officername { get; set; }
public string Designation { get; set; }
public string TourPdfPath { get; set; }
public string TourPPtPath { get; set; }
public string TourZipPath { get; set; }
public string Tourpdffilename { get; set; }
public string Tourpptfilename { get; set; }
public string Tourzipfilename { get; set; }
public int status { get; set; } = 1;
[NotMapped]
[Required]
[Display(Name ="Tour PDF")]
[RegularExpression(@"([a-zA-Z0-9\s_\\.\-:])+(.doc|.docx|.pdf)$",ErrorMessage ="Only PDF Files Allowed")]
public HttpPostedFileBase tourpdfupload { get; set; }
[NotMapped]
[Required]
[Display(Name = "Tour PPt")]
public HttpPostedFileBase tourpptupload { get; set; }
[NotMapped]
[Required]
[Display(Name = "Tour zip")]
public HttpPostedFileBase tourzipupload { get; set; }
}
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "FileId,Officername,Designation,TourPdfPath,TourPPtPath,TourZipPath,Tourpdffilename,Tourpptfilename,Tourzipfilename,status")] TourDetail tourDetail)
{
if (ModelState.IsValid)
{
db.TourDetails.Add(tourDetail);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tourDetail);
}
@model OnlineStationaryRegister.Models.TourDetail
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Tour", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>TourDetail</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Officername, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Officername, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Officername, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Designation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Designation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Designation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Upload PDF", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.tourpdfupload, new { type = "file" })
@Html.ValidationMessageFor(model => model.tourpdfupload, "", new { @class = "text-danger" })
@*<input type="file" name="tourpdfupload" />*@
</div>
</div>
<div class="form-group">
@Html.Label("Upload PPT", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.tourpptupload, new { type = "file" })
@Html.ValidationMessageFor(model => model.tourpptupload, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Upload Zip", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.tourzipupload, new { type = "file" })
@Html.ValidationMessageFor(model => model.tourzipupload, "", new { @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-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}