简体   繁体   English

MVC FilePathResult无法下载文件

[英]MVC FilePathResult not downloading file

FilePathResult is not downloading file FilePathResult未下载文件

I have tried returning File(...), tried changing the file to be a blank workbook, changed the location of the file 我尝试返回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;
        }

I would expect the file to download when this method is hit as no exceptions are thrown 我希望在点击此方法时下载文件,因为不会引发异常


Here is an updated version of the problem 这是问题的更新版本

Here is the ajax call with the downloadfile call removed 这是删除了filefile调用的ajax调用

$.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");
                }
            }
        );

Here is the new(er) probably slightly different, although it behaves the same, controller method 这是新的,虽然行为上相同,但可能略有不同,控制器方法

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;
    }

With either returns in the method, File or FilePathResult, I get the same behaviour that the file will not download. 使用File或FilePathResult方法中的任何一个返回,都会得到与文件不会下载相同的行为。 I have to be doing something wrong?? 我必须做错什么了?

use FileResult insted of action result 使用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;
    }

Did you tried to use the "File" method ? 您是否尝试使用“文件”方法? Like this : 像这样 :

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;
    }

It expects "filePath" to be the full path of the file (with name and extension) 它期望“ filePath”是文件的完整路径(带有名称和扩展名)

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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