2016-07-13 6 views
0

Visual Studio를 사용하여 ASP.NET MVC에서 파일을 다운로드하는 데 문제가 있습니다. 내 코드의 주요 목적은 내 로컬 파일 시스템에 라이센스 파일을 저장하는 것입니다. 내 업로드 기능이 정상적으로 작동하지만 다운로드 기능이 정상적으로 작동하지 않습니다. 내가 사용하는 데이터베이스가 목록을 저장할 수 없기 때문에 현재 각 파일 이름을 "9-0.lic, 9-1.lic, 9-2.lic"와 같은 문자열로 저장합니다. 첫 번째 숫자 (9)는 Software License Model의 기본 키이고 대시 다음의 숫자는 라이센스 파일 번호 (1, 2 및 3)입니다.여러 파일 다운로드 중. ASP.NET MVC C#

SoftwareLicense 모델 :

public class SoftwareLicense 
{ 
    [Key] 
    public int SoftwareID { get; set; } // Properties of class SoftwareLicense 

    public int UserID { get; set; } 
    [ForeignKey("UserID")] 
    public virtual IntranetUser User { get; set; } 

    [Required(ErrorMessage = "Software title field is required.")] 
    [Display(Name = "Software Title")] 
    public string SoftwareName { get; set; } 

    [Required(ErrorMessage = "The software key field is required")] 
    [Display(Name = "Software Key")] 
    public string SoftwareKey { get; set; } 

    [Required(ErrorMessage = "The software price field is required")] 
    [Display(Name = "Software Price")] 
    public decimal Price { get; set; } 

    [Display(Name = "License File")] 
    public string LicenseFileName { get; set; } 

    [Display(Name = "File Path")] 
    public string LicenseFilePath { get; set; } 

    [Required(ErrorMessage = "The start date field is required.")] 
    [Display(Name = "Start Date")] 
    public DateTime StartDate { get; set; } 

    [Required(ErrorMessage = "The end date field is required")] 
    [Display(Name = "End Date")] 
    public DateTime EndDate { get; set; } 

    [Required(ErrorMessage = "The license type field is required")] 
    [Display(Name = "License Type")] 
    public string LicenseType { get; set; } 

    [Display(Name="Department Name")] 
    public string DepartmentName { get; set; } 

    [Required(ErrorMessage = "The notify time field is required")] 
    [Display(Name = "Notify Time")] 
    public int NotifyTime { get; set; } 

    [Required(ErrorMessage = "The email field is required")] 
    [Display(Name = "Entry Created By")] 
    public string Email { get; set; } 

    public bool Active { get; set; } 
} 

PreDownload 방법 SoftwareLicense의 컨트롤러 : SoftwareLicense 컨트롤러에

public ActionResult PreDownload(int ID) 
    { 
     SoftwareLicense softwareLicense = db.SoftwareLicenses.Find(ID); 

     var fileNames = softwareLicense.LicenseFileName.Split(','); 

     foreach(string fileName in fileNames) 
     { 
      if(fileName.Length > 1) 
      { 
       Download(fileName, softwareLicense.SoftwareName); 
      } 
     } 
     SetUserInfo(); 
     return View("Details", softwareLicense); 
    } 

다운로드 방법 : 사람이 어떤 포인터 또는 솔루션이있는 경우

public FileResult Download(string fileName, string licenseName) 
    { 
     string returnName = licenseName + '-' + fileName; 

     if((fileName != "") || (fileName != " ")) 
     { 
      return File("C:/Users/heathera/Desktop/Licenses/" + fileName, MediaTypeNames.Application.Octet, returnName); 
     } 
     else 
     { 
      return null; 
     } 

    } 

나를 주시기 바랍니다 알다시피, 나는 하루나 이틀 동안 이것에 붙어있어 보일 수 없다. 그것을 알아 내라. 또한, 내 코드에 대한 지침이 있다면 항상 더 똑똑한 프로그래밍에 대해 배우려합니다.

미리 감사드립니다. Andrew.

+1

정확하게 당신이 "작동하지 않는"무엇을 의미합니까? – stuartd

+0

model-view-controller 태그는 패턴에 대한 질문입니다. ASP.NET-MVC 구현을위한 특정 태그가 있습니다. –

+1

여러 파일을 반환하려면 컨테이너에 넣는 것이 가장 좋습니다 (예 : zip 파일 – stuartd

답변

0

내 문제는 브라우저 문제로 인해 발생한 것으로 가정합니다. @ DavidG의 응답을 읽은 후, 다운로드해야하는 파일 이름 목록을 만들기 위해 SoftwareLicense Controller의 내 PreDownload 동작을 성공적으로 변경할 수있었습니다. 여기에서 "DownloadList"라는 새 뷰를 만들고 파일 이름이 첫 번째 열이되는 HTML 테이블을 만들고 ActionLink는 두 번째 열인 SoftwareLicense 컨트롤러의 다운로드 작업에 파일 이름을 전달합니다.

이 접근법은 완벽하게 작동하는 것으로 보입니다. 게다가 다운로드 작업을 크게 바꿀 필요가 없었습니다 (파일 이름과 라이센스 이름을 정의하기 위해 몇 줄을 변경해야했습니다)!

@DavidG!

는 *****

SoftwareLicense 모델은 변경되지 않았다 ***** 아래의 코드를 업데이트했습니다.

SoftwareLicense 컨트롤러에서

PreDownload 작업 : SoftwareLicense 컨트롤러에

public ActionResult PreDownload(int ID) 
    { 

     SoftwareLicense softwareLicense = db.SoftwareLicenses.Find(ID); 

     TempData["SoftwareID"] = softwareLicense.SoftwareID; 

     var fileNames = softwareLicense.LicenseFileName.Split(','); 

     List<string> downloadList = new List<string>(); 

     foreach(string fileName in fileNames) 
     { 
      if(fileName.Length > 1) 
      { 
       downloadList.Add(softwareLicense.SoftwareName + '_' + fileName); 
      } 
     } 
     SetUserInfo(); 
     return View("DownloadList", downloadList); 
    } 

다운로드 작업 :

public FileResult Download(string fileName) 
    { 
     string licenseName = fileName.Split('_')[1]; 

     string filePath = "C:/Users/heathera/Desktop/Licenses/" + licenseName; 

     string returnName = fileName; 

     if((fileName != "") || (fileName != " ")) 
     { 
      return File(filePath, MediaTypeNames.Application.Octet, returnName); 
     } 
     else 
     { 
      return null; 
     } 
    } 
+0

당신은 환영합니다 :) – DavidG