【问题标题】:Not able to prompt save dialog to dowload the zip file in MVC无法提示保存对话框以在 MVC 中下载 zip 文件
【发布时间】:2012-03-22 02:40:42
【问题描述】:

我的 MVC 视图中有 jqgrid,它显示了可用文件的数量。选中 jqGrid 每一行的复选框后,用户可以下载单个文件或 zip 格式的多个文件。单个文件下载工作正常并且能够提示用户保存文件但是当我以 zip 格式下载文件时,我没有得到任何保存提示来将文件保存在客户端计算机上。 Zip 文件在文件夹中创建正常,但不提示保存。我不知道我在哪里做错了。请参阅以下代码并在此问题上帮助我....

    **Controller**
    [HttpGet]
    public ActionResult DownloadZip(String[] filesToZip)
    {
        //checking if there any file to zip
        if (filesToZip == null || filesToZip.Length < 1 || filesToZip.Count() == 0)
            return (new EmptyResult());

        //creating dynamic zip file name
        var downloadFileName = string.Format("Test_{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));

        //zipping the files
        using (ZipOutputStream zipos = new ZipOutputStream(System.IO.File.Create(Path.Combine(Server.MapPath(uploadLocation), downloadFileName))))
        {
            // 0-9, 9 being the highest compression
            zipos.SetLevel(9);

            //temp buffer to hold 4gb data max
            byte[] buffer = new byte[4096];

            foreach (string file in filesToZip)
            {
                ZipEntry newEntry = new ZipEntry(Path.GetFileName(file));
                zipos.PutNextEntry(newEntry);

                using (FileStream fs = System.IO.File.OpenRead(file))
                {
                    int sourceBytes;
                    do
                    {
                        sourceBytes = fs.Read(buffer, 0, buffer.Length);
                        zipos.Write(buffer, 0, sourceBytes);
                    }
                    while (sourceBytes > 0);
                }
            }//end files loop
        }


        System.IO.FileInfo zipFile = new System.IO.FileInfo(Path.Combine(Server.MapPath(uploadLocation), downloadFileName));

        // clear the current output content from the buffer
        Response.Clear();

        // add the header that specifies the default filename for the 
        // Download/SaveAs dialog 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadFileName);

        // add the header that specifies the file size, so that the browser
        // can show the download progress
        Response.AddHeader("Content-Length", zipFile.Length.ToString());

        // specify that the response is a stream that cannot be read by the
        // client and must be downloaded
        Response.ContentType = "application/zip";
        // send the file stream to the client
        Response.WriteFile(zipFile.FullName);


        //ControllerContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + downloadFileName);
        //return File(Path.Combine(Server.MapPath(uploadLocation), downloadFileName), "application/zip", downloadFileName);

        return (new EmptyResult());
   }

   **View**

            $("#btnDownloadZip").click(function () {

                var files = $("#list").jqGrid('getGridParam', 'selarrrow').toString();

                if (files == '') {
                    alert('Please select a file to download...');
                    return;
                }

                var fileList = files.split(",");

                $.post('<%= Url.Action("DownloadZip") %>', { filesToZip: fileList }, handleSuccess);

                //$.getJSON("/Home/DownloadZip/", { filesToZip: fileList });
            });
     function handleSuccess()
     {
     }

【问题讨论】:

    标签: asp.net-mvc jqgrid


    【解决方案1】:

    Asp.Net MVC 有一个函数名称 File,您可以使用它从控制器操作中返回文件。

    请参阅this 显示如何返回文件的问题

    【讨论】:

    • 如果你看到我的代码,我已经注释了底部的第 3 行。我尝试使用 File 但在我的情况下不起作用,所以我尝试了不同的方式。当我下载单个文件时文件工作正常,但是当我下载 zip 文件时,文件工作正常......
    • $("#btnDownloadZip").click(function () { var files = $("#list").jqGrid('getGridParam', 'selarrrow').toString(); if ( files == '') { alert('请选择要下载的文件...'); return; } var fileList = files.split(","); $.post('', { filesToZip: fileList }, handleSuccess); //$.getJSON("/Home/DownloadZip/", { filesToZip: fileList }); });
    • 嗨,我解决了。我用 window.location = "/Home/DownloadZip?filesToZip=" + fileList;而不是 Url.Action 或 getJSON。谢谢....
    猜你喜欢
    • 2014-12-21
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2015-10-02
    相关资源
    最近更新 更多