简体   繁体   English

请求网址太长

[英]Request URL Too Long

I getting this error " Request URL Too Long " when i tried to download the file. 尝试下载文件时出现此错误“ Request URL Too Long ”。 The url shows the data that i want to download but it doesn't download. 网址显示了我要下载的数据,但没有下载。

How can i solve Request URL Too Long error when downloading 下载时如何解决请求网址太长的错误

my code 我的密码

Javascript Java脚本

 <script>
                    function DownloadIndexController(possID) {
                        $.ajax({
                            url: '@Url.Action("DownloadIndex", "Poss")',
                            contentType: 'application/json; charset=utf-8',
                            datatype: 'json',
                            data: { possID: possID },
                            type: "GET",
                            success: function (returnValue) {

                                window.location = '/DownloadIndex/Poss' + returnValue;
                            }
                        })
                    }
            </script>

Controller 控制者

  [HttpGet]
    public virtual ActionResult DownloadIndex(int possID)
    {
        try
        {
            possFilename = possFilename.Replace(",", ",");
            string fullPath = Filelocation + possFilename;
            return File(fullPath,  System.Net.Mime.MediaTypeNames.Application.Octet, possFilename);

        }
        catch (Exception ex)
        {
            throw ex;
        }

You cannot use ajax to download a file in that way. 您不能以这种方式使用ajax下载文件。 What you need to do is to generate a normal download link for every item in your grid, like: 您需要做的是为网格中的每个项目生成一个正常的下载链接,例如:

@Html.ActionLink("Download", "DownloadIndex", "Home", new { possID = "123" }, null)

Where Home is the name of your controller, and you have to dynamically add the possID for every item instead of the hard coded 123 in my example. 其中Home是控制器的名称,您必须为每个项目动态添加possID ,而不是在我的示例中为硬编码123

In your code, you download the file using ajax, and then you redirect to an url containing the full file just after '/DownloadIndex/Poss' 在代码中,您使用ajax下载文件,然后重定向到包含'/DownloadIndex/Poss'之后的完整文件的url '/DownloadIndex/Poss'

You probably just want to redirect to the file, without using Ajax at all : 您可能只想完全不使用Ajax重定向到文件:

<script>
    function DownloadIndexController(possID) {
        window.location = '/DownloadIndex/Poss?possID=' + possID;
    }
</script>

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

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