Example #1
0
 public void FileDownload(HttpResponseBase response,string filePah)
 {
     if(!IsExists(filePah)) {
         throw new Exception("�ļ�������");
     }
     var fileInfo = new FileInfo(filePah);
     response.Clear();
     response.ClearContent();
     response.ClearHeaders();
     response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileInfo.Name,System.Text.Encoding.UTF8));
     response.AddHeader("Content-Length", fileInfo.Length.ToString());
     //response.AddHeader("Content-Transfer-Encoding", "binary");
     response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
     response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
     response.WriteFile(fileInfo.FullName);
     response.Flush();
     response.End();
 }
 /// <summary>
 /// Exports the chart to the specified HttpResponse object. This method
 /// is preferred over WriteToStream() because it handles clearing the
 /// output stream and setting the HTTP reponse headers.
 /// </summary>
 /// <param name="httpResponse"></param>
 public void WriteToHttpResponse(HttpResponseBase httpResponse)
 {
     httpResponse.ClearContent();
     httpResponse.ClearHeaders();
     httpResponse.ContentType = this.ContentType;
     httpResponse.AddHeader("Content-Disposition", this.ContentDisposition);
     WriteToStream(httpResponse.OutputStream);
 }
Example #3
0
        protected override void WriteFile(HttpResponseBase response)
        {
            response.BufferOutput = false;

            response.Clear();
            response.ClearContent();
            response.ClearHeaders();
            response.Cookies.Clear();
            response.ContentType = ContentType;
            response.ContentEncoding = Encoding.Default;
            response.AddHeader("Content-Type", ContentType);
            response.AddHeader("Content-Disposition",
                                    String.Format("attachment; filename={0}",
                                    this.FileDownloadName));
            byte[] buffer = new byte[4096];

            using (ZipOutputStream zipOutputStream =
                        new ZipOutputStream(response.OutputStream))
            {
                foreach (string fileName in FilesToZip)
                {
                    int folderOffset = fileName.LastIndexOf('\\');

                    Stream fs = System.IO.File.OpenRead(fileName);    // or any suitable inputstream
                    string entryName = fileName.Substring(folderOffset);
                    ZipEntry entry = new ZipEntry(ZipEntry.CleanName(entryName));
                    entry.Size = fs.Length;
                    // Setting the Size provides WinXP built-in extractor compatibility,
                    //  but if not available, you can set zipOutputStream.UseZip64 = UseZip64.Off instead.

                    zipOutputStream.PutNextEntry(entry);
                    //zipOutputStream.SetLevel(3); // 0-9 for compression level
                    //zipOutputStream.Password(); // security

                    int count = fs.Read(buffer, 0, buffer.Length);
                    while (count > 0)
                    {
                        zipOutputStream.Write(buffer, 0, count);
                        count = fs.Read(buffer, 0, buffer.Length);
                        if (!response.IsClientConnected)
                        {
                            break;
                        }
                        response.Flush();
                    }
                    fs.Close();
                }
                zipOutputStream.Finish();
            }
            //response.OutputStream.Flush();
            response.End();

        }