Hi while using file upload control I am not getting the file path exactly. The control shows only the file name. If I take path using
<pre lang="c#">System.IO.Path.GetFullPath()
method it shows a location in c drive, but actually the file is from different drive. Can anyone please help me?
I am posting my code here.
Model
public class OrganizationJobApplication
{
public int OrganizationJobID { get; set; }
[Required(ErrorMessage = "Please enter name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter email")]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter mobile number")]
[RegularExpression(@"^[0-9]*$", ErrorMessage = "Please enter valid phone number")]
[StringLength(50, ErrorMessage = "Please enter valid phone number", MinimumLength = 10)]
public string Phone { get; set; }
[Required(ErrorMessage = "Please enter qualification")]
public string Qualification { get; set; }
public string SalaryExpected { get; set; }
[Required(ErrorMessage = "Please enter work experience")]
public int WorkExperience { get; set; }
public string Resume { get; set; }
[Required(ErrorMessage = "Please upload resume")]
public HttpPostedFileBase file { get; set; }
}
View
@using (Html.BeginForm("Apply", "WorkWithUs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
Html.ValidationSummary(true);
<div class="col-lg-8 col-md-8 col-sm-12">
<div class="form-horizontal">
<div>@Html.HiddenFor(model => model.OrganizationJobID)</div>
<div class="form-group">
<label for="Name" class="col-lg-3 col-md-3 col-sm-12 control-label">
Name</label>
<div class="col-lg-6 col-md-6 col-sm-12">
@Html.TextBoxFor(model => model.Name, new { @class = "form-control", placeholder = "Name" })
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
<label for="Phone" class="col-lg-3 col-md-3 col-sm-12 control-label">
Phone</label>
<div class="col-lg-6 col-md-6 col-sm-12">
@Html.TextBoxFor(model => model.Phone, new { @class = "form-control", placeholder = "Phone" })
@Html.ValidationMessageFor(model => model.Phone)
</div>
</div>
<div class="form-group">
<label for="Email" class="col-lg-3 col-md-3 col-sm-12 control-label">
Email</label>
<div class="col-lg-6 col-md-6 col-sm-12">
@Html.TextBoxFor(model => model.Email, new { @class = "form-control", placeholder = "Email" })
@Html.ValidationMessageFor(model => model.Email)
</div>
</div>
<div class="form-group">
<label for="Qualification" class="col-lg-3 col-md-3 col-sm-12 control-label">
Qualification</label>
<div class="col-lg-6 col-md-6 col-sm-12">
@Html.TextBoxFor(model => model.Qualification, new { @class = "form-control", placeholder = "Qualification" })
@Html.ValidationMessageFor(model => model.Qualification)
</div>
</div>
<div class="form-group">
<label for="SalaryExpected" class="col-lg-3 col-md-3 col-sm-12 control-label">
Salary Expected</label>
<div class="col-lg-6 col-md-6 col-sm-12">
@Html.TextBoxFor(model => model.SalaryExpected, new { @class = "form-control", placeholder = "Salary Expected" })
@Html.ValidationMessageFor(model => model.SalaryExpected)
</div>
</div>
<div class="form-group">
<label for="workExperience" class="col-lg-3 col-md-3 col-sm-12 control-label">
Work Experience</label>
<div class="col-lg-6 col-md-6 col-sm-10 col-xs-10">
@Html.TextBoxFor(model => model.WorkExperience, new { @class = "form-control", placeholder = "Work Experience" })
@Html.ValidationMessageFor(model => model.WorkExperience)
</div>
<div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 pull-left">
months</div>
</div>
<div class="form-group">
<label for="resume" class="col-lg-3 col-md-3 col-sm-12 control-label">
Upload Resume</label>
<div class="col-lg-6 col-md-6 col-sm-12">
@Html.TextBoxFor(model => model.file, new { @class = "form-control", placeholder = "Resume", type = "file",name="file" })
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-info col-lg-2" name="Save" id="btnSave">
Save</button>
</div>
</div>
</div>
}
Controller
[HttpPost]
public ActionResult Apply(OrganizationJobApplication jobApplicationRegister)
{
if (ModelState.IsValid)
{
Email email = new Email();
Attachment attachment;
if (jobApplicationRegister.file != null && jobApplicationRegister.file.ContentLength > 0)
{
jobApplicationRegister.Resume = System.IO.Path.GetFileName(jobApplicationRegister.file.FileName);
attachment = new Attachment(System.IO.Path.GetFullPath(jobApplicationRegister.file.FileName), MediaTypeNames.Application.Octet);
var path = Path.Combine(Server.MapPath("~/Content/Upload"), jobApplicationRegister.Resume);
jobApplicationRegister.file.SaveAs(path);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(jobApplicationRegister.Resume);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(jobApplicationRegister.Resume);
disposition.ReadDate = System.IO.File.GetLastAccessTime(jobApplicationRegister.Resume);
email.SendEmail("no-reply@info.in", "jobs@info.in", null, null, "Job Application - " + jobApplicationRegister.Qualification, "", attachment, "smtp.gmail.com", 587, true, "info@info.in", "12##info");
}
else
email.SendEmail("no-reply@info.in", "jobs@info.in", null, null, "Job Application - " + jobApplicationRegister.Qualification, "", "smtp.gmail.com", 587, true, "info@info.in", "12##info");
objSMWebDefaultConnection.smConn.spOrganizationJobApplicationEF(jobApplicationRegister.OrganizationJobID, jobApplicationRegister.Name, jobApplicationRegister.Phone, jobApplicationRegister.Email, jobApplicationRegister.Qualification, jobApplicationRegister.SalaryExpected, jobApplicationRegister.WorkExperience, jobApplicationRegister.Resume);
objSMWebDefaultConnection.smConn.SaveChanges();
ModelState.Clear();
return RedirectToAction("Index");
}
else
return View();
}