[英]Downloading Multiple Files; ASP.NET MVC C#
我在使用Visual Studio在ASP.NET MVC中下載文件時遇到問題。 我的代碼的主要目的是將許可文件存儲在本地文件系統中。 我的上傳功能運行正常,但是我的下載功能卻無法正常運行。 因為我正在使用的數據庫無法存儲列表,所以我目前將每個文件名存儲在一個字符串中,例如:“ 9-0.lic,9-1.lic,9-2.lic,”。 第一個數字(9)是我的SoftwareLicense Model的主鍵,破折號后的數字是許可證文件號(1、2和3)。
軟件許可模式:
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; }
}
SoftwareLicense Controller中的預下載方法:
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);
}
SoftwareLicense Controller中的下載方法:
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;
}
}
如果有人有任何指示或解決方案,請讓我知道,我已經堅持了這一天一兩天,似乎無法弄清楚。 另外,如果您對我的任何代碼都有任何指針,我總是願意學習更智能的編程。
提前致謝! 安德魯。
我假設我的問題是由於瀏覽器問題引起的。 閱讀@DavidG的回復后,我能夠成功更改SoftwareLicense Controller的PreDownload操作,以創建需要下載的文件名列表。 從那里,我創建了一個名為“ DownloadList”的新視圖,並創建了一個以文件名為第一列的HTML表,並以ActionLink將文件名傳遞給SoftwareLicense Controller中的Download動作為第二列。
這種方法似乎運行得很好,而且我不必大幅度地更改“下載”操作(盡管必須更改幾行來定義文件名和許可證名)!
謝謝@DavidG!
*****更新了下面的代碼*****
SoftwareLicense模型沒有更改。
SoftwareLicense Controller中的預下載操作:
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);
}
在SoftwareLicense Controller中的下載操作:
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.