2012-08-22 47 views
0

我目前有多個文件輸入的形式:如何識別來自多個html文件輸入的文件?

Resume: <input type="file" id="resume" name ="files" /><br /> 
Cover Letter: <input type="file" id="coverLetter" name ="files" /><br /> 

,並在我的後端:

[HttpPost] 
public ActionResult Apply(ApplyModel form, List<HttpPostedFileBase> files) 
{ 
    if (!ModelState.IsValid) 
    { 
     return View(form); 
    } 
    else if (files.All(x => x == null)) 
    { 
     ModelState.AddModelError("Files", "Missing Files"); 
     return View(form); 
    } 
    else 
    { 
     foreach (var file in files) 
     { 
      if (file.ContentLength > 0) 
      { 
       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(Server.MapPath("~/uploads"), fileName); 
       file.SaveAs(path); 
       <!--- HERE --> 
      } 
     } 
    } 
} 

我的問題是我如何確定該文件是從ID簡歷或求職信現場評論這裏。

回答

3

您無法識別它。您需要使用不同的名稱:

Resume: <input type="file" id="resume" name="coverLetter" /><br /> 
Cover Letter: <input type="file" id="coverLetter" name="resume" /><br /> 

然後:

[HttpPost] 
public ActionResult Apply(ApplyModel form, HttpPostedFileBase coverLetter, HttpPostedFileBase resume) 
{ 
    ... now you know how to identify the cover letter and the resume 
} 

,避免大量的動作參數使用視圖模型:

public class ApplicationViewModel 
{ 
    public ApplyModel Form { get; set; } 
    public HttpPostedFileBase CoverLetter { get; set; } 
    public HttpPostedFileBase Resume { get; set; } 
} 

然後:

[HttpPost] 
public ActionResult Apply(ApplicationViewModel model) 
{ 
    ... now you know how to identify the cover letter and the resume 
} 
+0

謝謝......我的想法是一廂情願。 –