2013-07-25 3 views
1

는 내가이 유용한 post 읽을 파일 업로드를 테스트 할 수있는 샘플 MVC 응용 프로그램을 작성하고 그것을 할 수 있지만 클라이언트 검증하지 작동하고 나 오류 준다 내 모든 코드는 다음과 같습니다파일 업 로더가 작동하지 않는 이유는 무엇입니까?

<script src="~/Scripts/jquery-1.8.2.min.js"></script> 
<script src="~/Scripts/jquery-ui-1.8.24.min.js"></script> 
<script src="~/Scripts/jquery.validate.min.js"></script> 
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script> 
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> 
:

내가 heder 태그에 이러한 첨부

내 모델 :

public class FIleModel 
{ 
    [Required, FileExtensions(Extensions = "csv", ErrorMessage = "Specify a CSV file. (Comma-separated values)")] 
    public HttpPostedFileBase myFile { get; set; } 
} 

오류 :

Unhandled exception at line 4, column 9003 in http://localhost:6284/Scripts  
/jquery.validate.min.js 
    0x800a138f - JavaScript runtime error: Unable to get property 'call' 
    of undefined or null reference 
내보기에

:

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
    { 
     @Html.ValidationSummary(); 
     <fieldset> 
      <legend>Registration Form</legend> 
      <ol> 
       <li class="lifile"> 
        @Html.TextBoxFor(m => m.myFile, new { type = "file" }) 
        @Html.ValidationMessageFor(m => m.myFile) 

       </li> 
      </ol> 
      <input type="submit" id="btnSubmit" value="Upload" /> 
     </fieldset> 
    } 
+0

는 http://stackoverflow.com/questions/14659023/error-in-jquery-validate-js-in에서보세요 -mvc-4-project-with-jquery-1- –

답변

0

는 파일 업로드를 들어, 다음 코드를 사용할 수 있습니다.

컨트롤러에서

:

[HttpPost] 
public ActionResult Create(EventModel eventmodel, HttpPostedFileBase file) 
{ 
    if (ModelState.IsValid) 
    { 

     //you can validate file here. if okay continue... 

     var filename = Path.GetFileName(file.FileName); 
     var path = Path.Combine(Server.MapPath("~/Uploads/Photo/"), filename); 
     file.SaveAs(path); 
     eventmodel.Url = filename; 

     _db.EventModels.AddObject(eventmodel); 
     _db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    return View(eventmodel); 
} 

을 그리고보기 :

<div> 
    Image 
    <input type="file" name="file" id="file" /> 
    @Html.HiddenFor(model => model.ImageUrl) 
    @Html.ValidationMessageFor(model => model.Url) 
</div> 
+0

타이어는 무엇입니까? – whisk

+1

내 대답이 바뀌 었습니다. 타이어는 하나의 Tire Company의 이전 코드에서 남았습니다. 주목 해 주셔서 감사합니다. –

관련 문제