Visual Studioを使用してASP.NET MVCでファイルをダウンロードする際に問題が発生しています。私のコードの主な目的は、ローカルファイルシステムにライセンスファイルを格納することです。私のアップロード機能はうまくいきますが、私のダウンロード機能はうまくいきません。私が使用しているデータベースはリストを格納することができないので、現在、各ファイル名は "9-0.lic、9-1.lic、9-2.lic"のように文字列に格納されています。最初の数字(9)は私のSoftwareLicense Modelのプライマリキーで、ダッシュの後の数字はライセンスファイル番号(1,2,3)です。複数のファイルをダウンロードする。 ASP.NET MVC C#
SoftwareLicenseモデル: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コントローラーで
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;
}
}
誰もが任意のポインタまたはソリューションを持っている場合は、私を聞かせてください知っている、私は1日か2日の間これに固執されているように見えないそれを理解する。また、私のコードに指針がある場合、私はいつもよりスマートなプログラミングについて知りたいと思っています。
ありがとうございます! Andrew。
正確にあなたが "動作しない" とはどういう意味ですか? – stuartd
model-view-controllerタグはパターンに関する質問のためのものです。 ASP.NET-MVCの実装には特定のタグがあります。 –
複数のファイルを返す場合は、コンテナに入れておくのがよいでしょう。例えば、zipファイル – stuartd