0

正如我的 ZIP 文件在文件夹中一样,如果我单击下载报告按钮,我会根据我的组织政策阻止下载。

但是我需要从代码中下载这个 ZIP 文件,我们如何才能做到这一点。

我使用的代码如下

string[] filenames = Directory.GetFiles(SourceFolder);

                ZipFilePath = DestinationFolder + @"\" + ZipFileName;
                using (ZipOutputStream s = new
                ZipOutputStream(File.Create(ZipFilePath)))
                {
                    s.SetLevel(6);
                    byte[] buffer = new byte[4096];

                    foreach (string file in filenames)
                    {
                        if (Path.GetFileName(file).Contains(SubString) || Path.GetFileName(file).Contains("logfile"))
                        {
                            ZipEntry entry = new
                            ZipEntry(Path.GetFileName(file));

                            entry.DateTime = DateTime.Now;
                            s.PutNextEntry(entry);
                            using (FileStream fs = File.OpenRead(file))
                            {
                                int sourceBytes;
                                do
                                {
                                    sourceBytes = fs.Read(buffer, 0,
                                    buffer.Length);

                                    s.Write(buffer, 0, sourceBytes);

                                } while (sourceBytes > 0);
                            }
                        }
                    }
                    s.Finish();
                    s.Close();
                }

                string DownloadFileName = ZipFilePath;
                DownloadFileName = DownloadFileName.Replace("\\", "~");
                RadAjaxManager1.ResponseScripts.Add("setTimeout(function(){ document.location.href = 'DownloadHandler.ashx?FileName=" + DownloadFileName + "'; return false; },300);");

DownloadHandler.ashx 页面如下

 public void ProcessRequest(HttpContext context)
    {
        try
        {
            HttpResponse rspns = context.Response;

            string FileToDownload = context.Request.QueryString["FileName"];

            string FileName = string.Empty;

            if (context.Request.QueryString["Name"] != null)
            {
                FileName = context.Request.QueryString["Name"];
            }
             if (FileToDownload!=null)
            {
                FileToDownload = FileToDownload.Replace("~", "\\");
                FileName = System.IO.Path.GetFileName(FileToDownload);
            }
            else 
            {

                //FileName = Convert.ToString(iTAPSession.UserData);
            }


            rspns.AppendHeader("content-disposition", "attachment; filename=\"" + FileName.Replace(" ", "%20"));
            rspns.TransmitFile(FileToDownload);
            rspns.End();

        }
        catch (Exception e)
        {

        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

收到以下异常

Based on your organization's access policies, access to this website or download ( http://xxxxxxx/ITAADemo/DownloadHandler.ashx?FileName=D:~ITAADemo~Files~SuperAdmin~bn4wgrusef1xgmjhqokd2yo2~~TextAnalytics~~zipdownload~Report_2018-Jul-19-11-39-31.zip ) has been blocked because the file type "application/zip" is not allowed.
4

0 回答 0