MVC FilePathResult无法下载文件

时间:2019-01-08 17:01:59

标签: c# asp.net-mvc download

FilePathResult未下载文件

我尝试返回File(...),尝试将文件更改为空白工作簿,更改了文件的位置

public ActionResult GenerateSupplyInformation(List<string> serialNumbers)
        {            
                if (serialNumbers != null)
                {
                    ViewBag.strmsg = "";
                    foreach (var serialNumber in serialNumbers)
                    {
                        if (!string.IsNullOrWhiteSpace(serialNumber))
                        {
                            var results = _ftRepository.GetSupplyInformation(serialNumber);

                            var filePath = _documentGenerator.GenerateSupplyInformation(serialNumber, results);

                            return new FilePathResult(filePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                        }
                    }
                }

            }

            return null;
        }

我希望在点击此方法时下载文件,因为不会引发异常


这是问题的更新版本

这里是ajax调用,但删除了downloadfile调用

$.ajax(
            {
                type: "POST",
                //url: "/JQueryAjaxCall/AjaxPostCall",
                url: "/RWM/GenerateSupplyInformation",
                data: JSON.stringify(models),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    //alert("response");
                }
            }
        );

这是新的,虽然行为上相同,但控制器方法可能略有不同

public ActionResult GenerateSupplyInformation(List<LxStockDataModel> records)
    {
        try
        {
            if (records != null)
            {
                ViewBag.strmsg = "";
                foreach (var serialNumber in records)
                {
                    if (!string.IsNullOrWhiteSpace(serialNumber.Serial_No))
                    {
                        var results = _ftRepository.GetSupplyInformation(serialNumber.Serial_No);

                        var filePath = _documentGenerator.GenerateSupplyInformation(serialNumber.Serial_No, results);

                        var file =  new FilePathResult(filePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                        //return file;
                        return File(filePath, System.Net.Mime.MediaTypeNames.Application.Octet);
                    }
                }
                return View("RWMSummary", records);
            }
        }
        catch (Exception ex)
        {
            _log.Exception(ex);
        }
        return null;
    }

通过方法File或FilePathResult中的任何一个返回,我将得到与文件不会下载相同的行为。我必须做错什么了?

2 个答案:

答案 0 :(得分:0)

使用FileResult插入动作结果

public FileResult GenerateSupplyInformation(List<string> serialNumbers)
    {            
            if (serialNumbers != null)
            {
                ViewBag.strmsg = "";
                foreach (var serialNumber in serialNumbers)
                {
                    if (!string.IsNullOrWhiteSpace(serialNumber))
                    {
                        var results = _ftRepository.GetSupplyInformation(serialNumber);

                        var filePath = _documentGenerator.GenerateSupplyInformation(serialNumber, results);

                        return new FilePathResult(filePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                    }
                }
            }

        }

        return null;
    }

答案 1 :(得分:0)

您是否尝试过使用“文件”方法?像这样:

public ActionResult GenerateSupplyInformation(List<string> serialNumbers)
    {            
            if (serialNumbers != null)
            {
                ViewBag.strmsg = "";
                foreach (var serialNumber in serialNumbers)
                {
                    if (!string.IsNullOrWhiteSpace(serialNumber))
                    {
                        var results = _ftRepository.GetSupplyInformation(serialNumber);

                        var filePath = _documentGenerator.GenerateSupplyInformation(serialNumber, results);

                        return File(filePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "YourDownloadedFileName.xls");
                    }
                }
            }

        }

        return null;
    }

它希望“ filePath”是文件的完整路径(带有名称和扩展名)

希望有帮助!