当前位置: 首页>>代码示例>>C#>>正文


C# HttpResponse.BinaryWrite方法代码示例

本文整理汇总了C#中HttpResponse.BinaryWrite方法的典型用法代码示例。如果您正苦于以下问题:C# HttpResponse.BinaryWrite方法的具体用法?C# HttpResponse.BinaryWrite怎么用?C# HttpResponse.BinaryWrite使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpResponse的用法示例。


在下文中一共展示了HttpResponse.BinaryWrite方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ResponseFile

 public bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullUrl, long _speed)
 {
     try
     {
         FileStream myFile = new FileStream(_fullUrl, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
         BinaryReader br = new BinaryReader(myFile);
         try
         {
             _Response.AddHeader("Accept-Ranges", "bytes");
             _Response.Buffer = false;
             long fileLength = myFile.Length;
             long startBytes = 0;
             int pack = 10240; //10K bytes
             int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;
             if (_Request.Headers["Range"] != null)
             {
                 _Response.StatusCode = 206;
                 string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
                 startBytes = Convert.ToInt64(range[1]);
             }
             _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
             if (startBytes != 0)
             {
                 _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
             }
             _Response.AddHeader("Connection", "Keep-Alive");
             _Response.ContentType = "application/octet-stream";
             _Response.AddHeader("Content-Disposition", "attachment;filename="
                                 + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
             br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
             int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
             for (int i = 0; i < maxCount; i++)
             {
                 if (_Response.IsClientConnected)
                 {
                     _Response.BinaryWrite(br.ReadBytes(pack));
                     Thread.Sleep(sleep);
                 }
                 else
                 {
                     i = maxCount;
                 }
             }
         }
         catch
         {
             return false;
         }
         finally
         {
             br.Close();
             myFile.Close();
         }
     }
     catch
     {
         return false;
     }
     return true;
 }
开发者ID:Raistlin97,项目名称:demo-projects,代码行数:60,代码来源:downloading.aspx.cs

示例2: ProcessImage

    private void ProcessImage(HttpApplication app, HttpResponse Response, HttpRequest Request)
    {
        string file = Request.Path.Substring(4).ToLower(); // stripping out the /cms part ...

        string output = "";

        Response.Clear();
        Response.ContentType = GetContentTypeOfFile(file);

        int newWidth = 0;
        int newHeight = 0;
        int maxWidth = 0;
        int maxHeight = 0;

        if (Request["w"] != null)
            newWidth = Convert.ToInt32(Request["w"].ToString());
        if (Request["h"] != null)
            newHeight = Convert.ToInt32(Request["h"].ToString());
        if (Request["mw"] != null)
            maxWidth = Convert.ToInt32(Request["mw"].ToString());
        if (Request["mh"] != null)
            maxHeight = Convert.ToInt32(Request["mh"].ToString());

        try
        {

            if (newWidth > 0 || newHeight > 0 || maxHeight > 0 || maxWidth > 0)
            {

                // resize and render the image

                // should cache it and reuse when possibl...

                Image i = Image.FromFile(Request.MapPath(Request.Path));

                using (MemoryStream ms = new MemoryStream())
                {

                    int newWidth2 = newWidth;
                    int newHeight2 = newHeight;

                    if (newWidth > 0 && newHeight == 0)
                        newHeight2 = Convert.ToInt32(((double)i.Height / (double)i.Width) * (double)newWidth);
                    else if (newHeight > 0 && newWidth == 0)
                        newWidth2 = Convert.ToInt32(((double)i.Width / (double)i.Height) * (double)newHeight);

                    if (newHeight2 == 0) newHeight2 = i.Height;
                    if (newWidth2 == 0) newWidth2 = i.Width;

                    if (maxWidth > 0 && newWidth2 > maxWidth)
                    {
                        newWidth2 = maxWidth;
                        newHeight2 = Convert.ToInt32(((double)i.Height / (double)i.Width) * (double)newWidth2);
                    }
                    if (maxHeight > 0 && newHeight2 > maxHeight)
                    {
                        newHeight2 = maxHeight;
                        newWidth2 = Convert.ToInt32(((double)i.Width / (double)i.Height) * (double)newHeight2);
                    }

                    Bitmap i2 = new Bitmap(i, newWidth2, newHeight2);

                    i2.Save(ms, GetImageFormatOfFile(file));

                    Response.BinaryWrite(ms.ToArray());
                }

            }
            else
            {
                // is there a way to call default handler instead of this??
                Response.WriteFile(Request.MapPath(Request.Path));
            }
        }
        catch
        {
            Response.WriteFile(Request.MapPath(Request.Path));
        }

        Response.End();
    }
开发者ID:ronmichael,项目名称:img2,代码行数:81,代码来源:ImgHandler.cs

示例3: ExportExcel

    /// <summary>
    /// Export excel with HTML format
    /// </summary>
    /// <param name="response">Current page response</param>
    /// <param name="fileName">export file name</param>
    /// <param name="tb">web html table</param>
    public static void ExportExcel(HttpResponse response, string fileName, Table tb)
    {
        try
        {
            response.Clear();
            response.ClearContent();
            response.ClearHeaders();
            response.Buffer = true;
            response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));

            response.Charset = "utf-8";
            response.ContentEncoding = System.Text.Encoding.UTF8;
            response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());

            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            tb.RenderControl(oHtmlTextWriter);
            response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
            response.Write(AddExcelStyling());
            string style = @"<style> .text { mso-number-format:\@; } </style>";
            response.Write(style);
            response.Write(oStringWriter.ToString());

            response.End();
        }
        catch (Exception ex)
        {
            Pollinator.Common.Logger.Error("Error occured at " + typeof(ImportExportUltility).Name + " ExportExcel().:", ex);
            response.End();
        }
    }
开发者ID:nazrulcse,项目名称:pollinatror,代码行数:39,代码来源:ImportExportUltility.cs

示例4: ExportCSV

    public static int numFieldOfImportFile = 16; //number column of import file

    #endregion Fields

    #region Methods

    /// <summary>
    /// Export data to csv and response directly
    /// </summary>
    /// <param name="response">Current page response</param>
    /// <param name="fileName">export file name</param>
    /// <param name="listData">list data to export</param>
    public static void ExportCSV(HttpResponse response, string fileName, List<ImportExportFields> listData)
    {
        //prepare the output stream
        response.Clear();
        response.Buffer = true;
        // response.ContentType = "text/csv";
        response.ContentType = "application/octet-stream";//application/vnd.ms-excel";
        response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));
        //response.ContentEncoding = Encoding.Unicode;
        response.ContentEncoding = System.Text.Encoding.UTF8;
        response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());

        response.Write(DataToCsvString(listData));

        response.Flush();
        response.End();
    }
开发者ID:nazrulcse,项目名称:pollinatror,代码行数:29,代码来源:ImportExportUltility.cs

示例5: OutputClientSireSelectionSheetsToPDF

    private static void OutputClientSireSelectionSheetsToPDF(LocalReport localReport, string fileName, bool landscape,
                                                             bool legal, HttpResponse response)
    {
        const string reportType = "PDF";
        string mimeType;
        string encoding;
        string fileNameExtension;

        // The DeviceInfo settings should be changed based on the reportType
        //      http://msdn2.microsoft.com/en-us/library/ms155397.aspx

        string deviceInfo =
            "<DeviceInfo>" +
            "  <OutputFormat>PDF</OutputFormat>" +
            "  <PageWidth>" + (landscape ? (legal ? "14" : "11") : "8.5") + "in</PageWidth>" +
            "  <PageHeight>" + (landscape ? "8.5" : (legal ? "14" : "11")) + "in</PageHeight>" +
            "  <MarginTop>0.3in</MarginTop>" +
            "  <MarginLeft>1.25in</MarginLeft>" +
            "  <MarginRight>0.25in</MarginRight>" +
            "  <MarginBottom>0.3in</MarginBottom>" +
            "</DeviceInfo>";

        /*  other attributes for the DeviceInfo are
            StartPage - The first page of the report to render. A value of 0 indicates that all pages are rendered. The default value is 1.
            Columns - The number of columns to set for the report. This value overrides the report's original settings.
            ColumnSpacing - The column spacing to set for the report. This value overrides the report's original settings.
            EndPage - The last page of the report to render. The default value is the value for StartPage.
        */

        Warning[] warnings;
        string[] streams;

        //Render the report
        byte[] renderedBytes = localReport.Render(
            reportType,
            deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);

        // Clear the response stream and write the bytes to the outputstream
        // Set content-disposition to "attachment" so that user is prompted to take an action
        // on the file (open or save)
        response.Clear();
        response.ContentType = mimeType;
        response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + fileNameExtension);
        response.BinaryWrite(renderedBytes);
        response.End();
    }
开发者ID:BeefboosterDevelopment,项目名称:Intranet,代码行数:51,代码来源:Report404.ascx.cs


注:本文中的HttpResponse.BinaryWrite方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。