简体   繁体   English

下载多个文件; ASP.NET MVC C#

[英]Downloading Multiple Files; ASP.NET MVC C#

I am having issues with downloading files in ASP.NET MVC using Visual Studio. 我在使用Visual Studio在ASP.NET MVC中下载文件时遇到问题。 The main purpose of my code is to store licensing files in my local file system. 我的代码的主要目的是将许可文件存储在本地文件系统中。 My upload function is working fine, however, my download function is not. 我的上传功能运行正常,但是我的下载功能却无法正常运行。 Because the database I am using is unable to store lists, I currently store each file name in a string as such: "9-0.lic,9-1.lic,9-2.lic,". 因为我正在使用的数据库无法存储列表,所以我目前将每个文件名存储在一个字符串中,例如:“ 9-0.lic,9-1.lic,9-2.lic,”。 The first number (9) is the primary key for my SoftwareLicense Model, and the number after the dash is the license file number (1, 2 and 3). 第一个数字(9)是我的SoftwareLicense Model的主键,破折号后的数字是许可证文件号(1、2和3)。

SoftwareLicense Model: 软件许可模式:

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 Method in SoftwareLicense Controller: 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);
    }

Download Method in SoftwareLicense Controller: 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;
        }

    }

If anyone has any pointers or solutions please let me know, I have been stuck on this for a day or two now and can't seem to figure it out. 如果有人有任何指示或解决方案,请让我知道,我已经坚持了这一天一两天,似乎无法弄清楚。 Also, if you have any pointers on any of my code I am always willing to learn about smarter programming. 另外,如果您对我的任何代码都有任何指针,我总是愿意学习更智能的编程。

Thanks in advance! 提前致谢! Andrew. 安德鲁。

I am assuming that my issues were caused due to a browser issue. 我假设我的问题是由于浏览器问题引起的。 After reading @DavidG's response, I was able to successfully alter my PreDownload action of the SoftwareLicense Controller to create a List of file names that need to be downloaded. 阅读@DavidG的回复后,我能够成功更改SoftwareLicense Controller的PreDownload操作,以创建需要下载的文件名列表。 From there, I created a new view named "DownloadList" and created an HTML table with the file names being the first column, and an ActionLink passing the file name to the Download action in the SoftwareLicense Controller being the second column. 从那里,我创建了一个名为“ DownloadList”的新视图,并创建了一个以文件名为第一列的HTML表,并以ActionLink将文件名传递给SoftwareLicense Controller中的Download动作为第二列。

This approach appears to be working perfectly, plus I didn't have to change my Download action drastically (had to alter a few lines to define file name and license name though)! 这种方法似乎运行得很好,而且我不必大幅度地更改“下载”操作(尽管必须更改几行来定义文件名和许可证名)!

Thank you @DavidG! 谢谢@DavidG!

***** Updated Code Below ***** *****更新了下面的代码*****

SoftwareLicense Model did not change. SoftwareLicense模型没有更改。

PreDownload Action in SoftwareLicense Controller: 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);
    }

Download Action in SoftwareLicense Controller: 在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM