Step 1: Create Model Class file
Models
public DateTime submission_deadline_date { get; set; }
public string period_covered { get; set; }
public HttpPostedFileBase File { get; set; }
public string fk_level_register { get; set; }
public DateTime date_modified { get; set; }
public string done_by { get; set; }
Step 2: Controllers create new ActionResult with crate class parameter as like below
HomeControler
you can also download source code from git hub repository click here
HomeControler
[HttpPost]
public ActionResult Create(Period collection)
{
string fpath = "";
try
{
if (Request.Files.Count > 0)
{
try
{
HttpPostedFileBase file = collection.File;
string fname;
// want to check extension then use this method
//if (CheckExtension(file.ContentType.ToLower()))
//{
// Checking for Internet Explorer
string extension = System.IO.Path.GetExtension(file.FileName);
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = Guid.NewGuid() + extension; //+ testfiles[testfiles.Length - 1];
}
else
{
fname = Guid.NewGuid() + extension; //+ file.FileName;
}
// Get the complete folder path and store the file inside it.
fname = System.IO.Path.Combine(Server.MapPath("~/Content/"), fname);
file.SaveAs(fname);
return Content("Successfully Uploaded");
//}
}
catch (Exception ex)
{
return Content("Error occurred. Error details: " + ex.Message);
}
}
}
catch (Exception)
{
return Content(Response.StatusCode.ToString() + " Bad Requrest error." + fpath);
}
return Content("No files selected.");
}
public bool CheckExtension(string Ext)
{
if (Ext == "application/pdf")
{
return true;
}
else if (Ext == "text/plain")
{
return true;
}
else if (Ext == "application/msword")
{
return true;
}
else if (Ext == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
return true;
}
else
{
return false;
}
}
Step 3: In View page add following code@using (Html.BeginForm("Create", "Home", null, FormMethod.Post, new { id = "formU", enctype = "multipart/form-data" })){
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.fk_level_register)
@Html.HiddenFor(model => model.date_modified)
@Html.HiddenFor(model => model.done_by)
<label for="file">Filename:</label>
<input type="file" name="File" id="File" style="width:240px" />
@*@Html.EditorFor(model => model.File, new { htmlAttributes = new { @type="file", @class = "form-control", @placeholder = "File path" } })*@
<div class="form-group">
@Html.LabelFor(model => model.submission_deadline_date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.submission_deadline_date)
@Html.ValidationMessageFor(model => model.submission_deadline_date)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.period_covered, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.period_covered, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.period_covered)
</div>
</div>
<div class="form-group">
<input id="submit" type="submit" value="Submit" class="btn btn-success" />
</div>
</div>
</div>}
you can also download source code from git hub repository click here
No comments:
Post a Comment