javascript 路径错误中的非法字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19190645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:43:08  来源:igfitidea点击:

illegal characters in path error

c#javascriptasp.net-mvcexcel

提问by user793468

I am trying to launch an excel file but get an error, Am I missing something?

我正在尝试启动一个 excel 文件但出现错误,我是否遗漏了什么?

I try to launch an excel file but get:

我尝试启动一个 excel 文件,但得到:

illegal characters in path

路径中的非法字符

Controller Action:

控制器动作:

    public ActionResult ExportData(DateTime Date)
    {

        return File("~\Reports\ExcelExport.xlsm?Date=" + Date, "application/vnd.ms-excel" , Server.UrlEncode("~\Reports\ExcelExport.xlsm?Date=" + Date));

    }

JavaScript:

JavaScript:

function ExportToExcel() {

    var link = '/Report/ExportData';
    var Date= $("#Date").val();
    $.ajax({
        url: link,
        contentType: 'application/json; charset=utf-8',
        data: { Date: Date},
        success: function (result) {                
        },
        error: function (result) {

        }
    });

};

回答by wagesj45

You're including the string value of the DateTime parameter in your path. That will look something like this: 10/4/2013 5:00:17 PM. Both /and :are invalid characters in a Windows path.

您在路径中包含 DateTime 参数的字符串值。这看起来像这样:10/4/2013 5:00:17 PM。无论/是在Windows路径无效字符。

回答by John

回答by Moho

You're passing in a hybrid URL/physical file path. Try something like this to get the physical file path if it's on disk:

您传入的是混合 URL/物理文件路径。如果它在磁盘上,请尝试这样的操作来获取物理文件路径:

string filePath = Server.MapPath(Url.Content("~/Reports/ExcelExport.xlsm"));