为什么ASP.net MVC4的fileupload控件中的文件路径出错? [英] Why there is an error in file path in fileupload control of ASP.net MVC4?
问题描述
在使用文件上传控件时,我没有准确地获取文件路径。该控件仅显示文件名。如果我使用
< pre lang =c#> System.IO.Path.GetFullPath()
方法获取路径它会在c中显示一个位置驱动器,但实际上该文件来自不同的驱动器。有人可以帮帮我吗?
我在这里发布我的代码。
模型
public class OrganizationJobApplication
{
public int OrganizationJobID {获得跨度>; set ; }
[必需(ErrorMessage = 请输入名称)]
public string 名称{获取 ; set ; }
[必需(ErrorMessage = 请输入电子邮件)]
[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 = 请输入有效的电子邮件地址)]
public string 电子邮件{ get ; set ; }
[必需(ErrorMessage = 请输入手机号码)]
[RegularExpression( @ ^ [0-9] * $,ErrorMessage = < span class =code-string> 请输入有效的电话号码)]
[StringLength( 50 ,ErrorMessage = 请输入有效的电话号码,MinimumLength = < span class =code-digit> 10 )]
public string 电话{获取; set ; }
[必需(ErrorMessage = 请输入资格)]
public string 资格认证{获取 ; set ; }
public string SalaryExpected {获得跨度>; set ; }
[必需(ErrorMessage = 请输入工作经验)]
public int WorkExperience { get ; set ; }
public string 恢复{获得跨度>; set ; }
[必需(ErrorMessage = 请上传简历)]
< span class =code-comment> // [ValidateFile]
public HttpPostedFileBase文件{ get ; set ; }
}
查看
@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 =Nameclass =col-lg-3 col-md-3 col -sm-12 control-label>
名称< / 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.Na me)
< / div>
< / div>
< div class =form-group>
< label for =Phoneclass =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 =Emailclass =col-lg-3 col-md-3 col-sm-12 control-label>
电子邮件< / 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 =Qualificationclass =col -lg-3 col-md-3 col-sm-12 con trol-label>
资格< / 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 =SalaryExpectedclass =col-lg-3 col-md-3 col-sm-12 control-label>
预期薪资< / label>
< div class =col-lg-6 col-md-6 col-sm-12>
@ Html.TextBoxFor(model => model.SalaryExpected,new {@class =form-control,占位符=预期薪水})
@ Html.ValidationMessageFor(model => model.SalaryExpected)
< / div>
< / div>
< div class =form-group>
< label for =workExperienceclass =col-lg-3 col-md-3 col-sm-12 control-label>
工作经验< / 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>
个月< / div>
< / div>
< div class =form-group>
< label for =resumeclass =col-lg-3 col-md-3 col-sm-12 control-label>
上传简历< / 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>
&l t; div class =form-group>
< button type =submitclass =btn btn-info col-lg-2name =saveid =btnSave >
保存< /按钮>
< / div>
< / div>
< / div>
}
控制器
[HttpPost]
public ActionResult Apply(OrganizationJobApplication jobApplicationRegister)
{
< span class =code-keyword> if (ModelState.IsValid)
{
Email email = new Email();
// 为此电子邮件创建文件附件。
附件附件;
if (jobApplicationRegister.file!= null && jobApplicationRegister.file.ContentLength < span class =code-keyword>> 0 )
{
jobApplicationRegister.Resume = System.IO.Path。用GetFileName(jobApplicationRegister.file.FileName);
attachment = new 附件(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 , 作业应用程序 - + jobApplicationRegister。资格认证, ,附件, smtp.gmail.com, 587 , true , info@info.in, 12 ## info);
}
else
email.SendEmail( no-reply@info.in, jobs@info.in , null , null , 作业应用程序 - + 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();
}
,ErrorMessage = 请输入有效的电子邮件地址)]
public 字符串电子邮件{获取; 设置;}
[必需(ErrorMessage = 请输入手机号码)]
[ RegularExpression( @ ^ [0-9] *
,ErrorMessage = 请输入有效的电话号码)]
[StringLength( 50 ,ErrorMessage = 请输入有效的电话号码,MinimumLength = 10 )]
public string 电话{获得跨度>; set ; }
[必需(ErrorMessage = 请输入资格)]
public string 资格认证{获取 ; set ; }
public string SalaryExpected {获得跨度>; set ; }
[必需(ErrorMessage = 请输入工作经验)]
public int WorkExperience { get ; set ; }
public string 恢复{获得跨度>; set ; }
[必需(ErrorMessage = 请上传简历)]
< span class =code-comment> // [ValidateFile]
public HttpPostedFileBase文件{ get ; set ; }
}
查看
@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 =Nameclass =col-lg-3 col-md-3 col -sm-12 control-label>
名称< / 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.Na me)
< / div>
< / div>
< div class =form-group>
< label for =Phoneclass =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 =Emailclass =col-lg-3 col-md-3 col-sm-12 control-label>
电子邮件< / 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 =Qualificationclass =col -lg-3 col-md-3 col-sm-12 con trol-label>
资格< / 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 =SalaryExpectedclass =col-lg-3 col-md-3 col-sm-12 control-label>
预期薪资< / label>
< div class =col-lg-6 col-md-6 col-sm-12>
@ Html.TextBoxFor(model => model.SalaryExpected,new {@class =form-control,占位符=预期薪水})
@ Html.ValidationMessageFor(model => model.SalaryExpected)
< / div>
< / div>
< div class =form-group>
< label for =workExperienceclass =col-lg-3 col-md-3 col-sm-12 control-label>
工作经验< / 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>
个月< / div>
< / div>
< div class =form-group>
< label for =resumeclass =col-lg-3 col-md-3 col-sm-12 control-label>
上传简历< / 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>
&l t; div class =form-group>
< button type =submitclass =btn btn-info col-lg-2name =saveid =btnSave >
保存< /按钮>
< / div>
< / div>
< / div>
}
控制器
[HttpPost](ModelState.IsValid)
public ActionResult Apply(OrganizationJobApplication jobApplicationRegister)
{
< span class =code-keyword> if
{
Email email = new Email();
// 为此电子邮件创建文件附件。
附件附件;
if (jobApplicationRegister.file!= null && jobApplicationRegister.file.ContentLength < span class =code-keyword>> 0 )
{
jobApplicationRegister.Resume = System.IO.Path。用GetFileName(jobApplicationRegister.file.FileName);
attachment = new 附件(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 , 作业应用程序 - + jobApplicationRegister。资格认证, ,附件, smtp.gmail.com, 587 , true , info@info.in, 12 ## info);
}
else
email.SendEmail( no-reply@info.in, jobs@info.in , null , null , 作业应用程序 - + 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();
}
使用Server.MapPath()而不是System.IO.Path.GetFullPath()来获取所请求应用程序的文件系统路径路径。
字符串 absFile = Server.MapPath(file);
< br $> b $ b或
string absFile = HttpContext.Current.Server.MapPath(file );
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")]
//[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
[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();
}
", 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
[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(); }
Use Server.MapPath() instead of System.IO.Path.GetFullPath() to get the file system path for a requested application path.
string absFile = Server.MapPath(file);
or
string absFile = HttpContext.Current.Server.MapPath(file);
这篇关于为什么ASP.net MVC4的fileupload控件中的文件路径出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!