2014-03-06 4 views
1

업로드 파일 용으로이 코드를 만들었습니다. 하지만 내가 만든 App_Data/Uploads 폴더에는 파일이 업로드되지 않습니다. 다음MVC에서 파일 업로드 및 링크 만들기 파일을 다운로드하십시오.

In view>> 

<form action="~/Views/Home/_SaveUpdate" method="post" enctype="multipart/form-data"> 

    <label for="file1">Filename:</label> 
    <input type="file" name="files" id="file1" /> 

    <label for="file2">Filename:</label> 
    <input type="file" name="files" id="file2" /> 

    <input type="submit" /> 
</form> 

And this my Handler>> 

[HttpPost] 
     public ActionResult Index(IEnumerable<HttpPostedFileBase> files) 
     { 
      foreach (var file in files) 
      { 
       if (file.ContentLength > 0) 
       { 
        var fileName = Path.GetFileName(file.FileName); 
        var path = Path.Combine(HttpContext.Server.MapPath("~/App_Data/Uploads"), fileName); 
        file.SaveAs(path); 
       } 
      } 
      return RedirectToAction("Index"); 
     } 

이 더 필요 할 무엇을 가르쳐주세요 >> 코드입니다. 또한 링크를 생성하여 파일을 다운로드하는 방법.

답변

0

처음에는 양식의 동작이 ~/Views/Home/_SaveUpdate을 가리키는 반면,이 동작은 사용자의 후 조치에 따라 /Home/Index이어야합니다. 둘째, App_Data 폴더 내에 Upload 폴더를 만들 었는지 확인하십시오. 업로드 문제를 처리해야합니다.

보기 :

<form action="/Home/Index" method="post" enctype="multipart/form-data"> 
    <label for="file1">Filename:</label> 
    <input type="file" name="files" id="file1" /> 
    <label for="file2">Filename:</label> 
    <input type="file" name="files" id="file2" /> 
    <input type="submit" /> 
</form> 

모든 업로드 된 파일에 다운로드 링크를 표시 할 경우, 당신은 App_Data가 아닌 다른 폴더에 이미지를 저장해야합니다. 보안상의 이유로 App_Data 폴더에 직접 액세스 할 수 없습니다.

디렉토리에있는 파일을 보여주는 좋은 예는

0

here을 찾을 수 있습니다 나는 당신의 코드를 가져다가 그것을 테스트하기 위해 몇 가지 변경을 .. 그것은했다.

테스트를 위해 방금 작업 이름을 변경했습니다. 양식이 올바 릅니까? 그것은 당신의 핸들러의 이름과 일치하지 않기 때문에

<form action="~/Views/Home/_SaveUpdate" 

:

public ActionResult Index(IEnumerable .... 

내 시험 : 확인하여 업로드 폴더가 있는지 확인하거나 예외를 얻을 것이다. 핸들러 :

 [HttpPost] 
    public ActionResult FileUploadPost(IEnumerable<HttpPostedFileBase> files) 
    { 
     foreach (var file in files) 
     { 
      if (file.ContentLength > 0) 
      { 
       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(HttpContext.Server.MapPath("~/Uploads"), fileName); 
       file.SaveAs(path); 
      } 
     } 
     return RedirectToAction("Index"); 
    } 

보기 :

<form action="FileUploadPost" method="post" enctype="multipart/form-data"> 

     <label for="file1">Filename1:</label> 
     <input type="file" name="files" id="file1" /> 

     <label for="file2">Filename2:</label> 
     <input type="file" name="files" id="file2" /> 

     <input type="submit" /> 
    </form> 
관련 문제