Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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")]
        //[ValidateFile]
        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

C#
[HttpPost]
        public ActionResult Apply(OrganizationJobApplication jobApplicationRegister)
        {
            if (ModelState.IsValid)
            {
                Email email = new Email();
                // Create the file attachment for this e-mail message.
                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);

                    // Add time stamp information for the file.
                    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();
        }
Posted
Updated 26-Nov-15 21:06pm
v4

1 solution

Use Server.MapPath() instead of System.IO.Path.GetFullPath() to get the file system path for a requested application path.
C#
string absFile = Server.MapPath(file);

or
C#
string absFile = HttpContext.Current.Server.MapPath(file);
 
Share this answer
 
Comments
Jineesh TR 27-Nov-15 3:13am    
Thanks,
It is working fine for me now.
[no name] 27-Nov-15 3:47am    
thanks
Jineesh TR 27-Nov-15 3:19am    
Sorry,

It is actually showing the current server path only.
I am uploading a file from another location.
help me please.
[no name] 27-Nov-15 3:22am    
from where you are uploading? You are selecting file from your local PC ?
Jineesh TR 27-Nov-15 3:56am    
yes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900