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


C# FileStream.ToArray方法代码示例

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


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

示例1: GetResponseAttachment

        /// <summary>
        /// The get response attachment.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        private void GetResponseAttachment([NotNull] HttpContext context)
        {
            try
            {
                // AttachmentID
                using (DataTable dt = this.GetRepository<Attachment>().List(null, context.Request.QueryString.GetFirstOrDefaultAs<int>("a"), null, 0, 1000))
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        // TODO : check download permissions here
                        if (!this.CheckAccessRights(row["BoardID"], row["MessageID"]))
                        {
                            // tear it down
                            // no permission to download
                            context.Response.Write(
                                "You have insufficient rights to download this resource. Contact forum administrator for further details.");
                            return;
                        }

                        byte[] data;

                        if (row.IsNull("FileData"))
                        {
                            string sUpDir = YafBoardFolders.Current.Uploads;

                            string oldFileName =
                                context.Server.MapPath(
                                    "{0}/{1}.{2}".FormatWith(sUpDir, row["MessageID"], row["FileName"]));
                            string newFileName =
                                context.Server.MapPath(
                                    "{0}/{1}.{2}.yafupload".FormatWith(sUpDir, row["MessageID"], row["FileName"]));

                            // use the new fileName (with extension) if it exists...
                            string fileName = File.Exists(newFileName) ? newFileName : oldFileName;

                            using (var input = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                data = input.ToArray();
                                input.Close();
                            }
                        }
                        else
                        {
                            data = (byte[])row["FileData"];
                        }

                        context.Response.ContentType = row["ContentType"].ToString();
                        context.Response.AppendHeader(
                            "Content-Disposition",
                            "attachment; filename={0}".FormatWith(
                                HttpUtility.UrlPathEncode(row["FileName"].ToString()).Replace("+", "_")));
                        context.Response.OutputStream.Write(data, 0, data.Length);
                        this.GetRepository<Attachment>().IncrementDownloadCounter(context.Request.QueryString.GetFirstOrDefaultAs<int>("a"));
                        break;
                    }
                }
            }
            catch (Exception x)
            {
                this.Get<ILogger>().Log(0, this, x, EventLogTypes.Information);
                context.Response.Write(
                    "Error: Resource has been moved or is unavailable. Please contact the forum admin.");
            }
        }
开发者ID:jiahello,项目名称:MyForum,代码行数:70,代码来源:YafResourceHandler.cs

示例2: GetResponseImage

        // TommyB: Start MOD: PreviewImages   ##########

        /// <summary>
        /// Gets the response image.
        /// </summary>
        /// <param name="context">The context.</param>
        private void GetResponseImage([NotNull] HttpContext context)
        {
            try
            {
                var eTag = @"""{0}""".FormatWith(context.Request.QueryString.GetFirstOrDefault("i"));

                if (context.Request.QueryString.GetFirstOrDefault("editor") == null)
                {
                    // add a download count...
                    this.GetRepository<Attachment>()
                        .IncrementDownloadCounter(context.Request.QueryString.GetFirstOrDefaultAs<int>("i"));
                }

                if (CheckETag(context, eTag))
                {
                    // found eTag... no need to resend/create this image 
                   return;
                }

                // AttachmentID
                var attachment =
                    this.GetRepository<Attachment>()
                        .ListTyped(attachmentID: context.Request.QueryString.GetFirstOrDefaultAs<int>("i"))
                        .FirstOrDefault();

                var boardID = context.Request.QueryString.GetFirstOrDefault("b") != null
                                  ? context.Request.QueryString.GetFirstOrDefaultAs<int>("b")
                                  : YafContext.Current.BoardSettings.BoardID;

                // check download permissions here
                if (!this.CheckAccessRights(boardID, attachment.MessageID))
                {
                    // tear it down
                    // no permission to download
                    context.Response.Write(
                        "You have insufficient rights to download this resource. Contact forum administrator for further details.");
                    return;
                }

                byte[] data;

                if (attachment.FileData == null)
                {
                    var uploadFolder = YafBoardFolders.Current.Uploads;

                    var oldFileName =
                         context.Server.MapPath(
                             "{0}/{1}.{2}".FormatWith(
                                 uploadFolder,
                                 attachment.MessageID > 0
                                     ? attachment.MessageID.ToString()
                                     : "u{0}".FormatWith(attachment.UserID),
                                 attachment.FileName));

                    var newFileName =
                        context.Server.MapPath(
                            "{0}/{1}.{2}.yafupload".FormatWith(
                                uploadFolder,
                                attachment.MessageID > 0
                                    ? attachment.MessageID.ToString()
                                    : "u{0}-{1}".FormatWith(attachment.UserID, attachment.ID),
                                attachment.FileName));

                    var fileName = oldFileName;

                    if (File.Exists(oldFileName))
                    {
                        fileName = oldFileName;

                    }
                    else
                    {
                        oldFileName =
                        context.Server.MapPath(
                            "{0}/{1}.{2}.yafupload".FormatWith(
                                uploadFolder,
                                attachment.MessageID > 0
                                    ? attachment.MessageID.ToString()
                                    : "u{0}".FormatWith(attachment.UserID),
                                attachment.FileName));

                        // use the new fileName (with extension) if it exists...
                        fileName = File.Exists(newFileName) ? newFileName : oldFileName;
                    }

                    using (var input = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        data = input.ToArray();
                        input.Close();
                    }
                }
                else
                {
                    data = attachment.FileData;
//.........这里部分代码省略.........
开发者ID:Pathfinder-Fr,项目名称:YAFNET,代码行数:101,代码来源:YafResourceHandler.cs

示例3: GetResponseImage

        // TommyB: Start MOD: PreviewImages   ##########

        /// <summary>
        /// The get response image.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        private void GetResponseImage([NotNull] HttpContext context)
        {
            try
            {
                string eTag = @"""{0}""".FormatWith(context.Request.QueryString.GetFirstOrDefault("i"));

                if (CheckETag(context, eTag))
                {
                    // found eTag... no need to resend/create this image -- just mark another view?
                    this.GetRepository<Attachment>().Download(context.Request.QueryString.GetFirstOrDefaultAs<int>("i"));
                    return;
                }

                // AttachmentID
                using (DataTable dt = this.GetRepository<Attachment>().List(null, context.Request.QueryString.GetFirstOrDefaultAs<int>("i"), null, 0, 1000))
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        // TODO : check download permissions here
                        if (!this.CheckAccessRights(row["BoardID"], row["MessageID"]))
                        {
                            // tear it down
                            // no permission to download
                            context.Response.Write(
                                "You have insufficient rights to download this resource. Contact forum administrator for further details.");
                            return;
                        }

                        byte[] data;

                        if (row.IsNull("FileData"))
                        {
                            string sUpDir = YafBoardFolders.Current.Uploads;

                            string oldFileName =
                                context.Server.MapPath(
                                    "{0}/{1}.{2}".FormatWith(sUpDir, row["MessageID"], row["FileName"]));
                            string newFileName =
                                context.Server.MapPath(
                                    "{0}/{1}.{2}.yafupload".FormatWith(sUpDir, row["MessageID"], row["FileName"]));

                            // use the new fileName (with extension) if it exists...
                            string fileName = File.Exists(newFileName) ? newFileName : oldFileName;

                            using (var input = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                data = input.ToArray();
                                input.Close();
                            }
                        }
                        else
                        {
                            data = (byte[])row["FileData"];
                        }

                        context.Response.ContentType = row["ContentType"].ToString();
                        context.Response.Cache.SetCacheability(HttpCacheability.Public);
                        context.Response.Cache.SetETag(eTag);
                        context.Response.OutputStream.Write(data, 0, data.Length);

                        // add a download count...
                        this.GetRepository<Attachment>().Download(context.Request.QueryString.GetFirstOrDefaultAs<int>("i"));
                        break;
                    }
                }
            }
            catch (Exception x)
            {
                this.Get<ILogger>().Log(0, this, x, EventLogTypes.Information);
                context.Response.Write(
                    "Error: Resource has been moved or is unavailable. Please contact the forum admin.");
            }
        }
开发者ID:RH-Code,项目名称:YAFNET,代码行数:81,代码来源:YafResourceHandler.cs

示例4: GetResponseAttachment

        /// <summary>
        /// The get response attachment.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        private void GetResponseAttachment([NotNull] HttpContext context)
        {
            try
            {
                // AttachmentID
                var attachment =
                    this.GetRepository<Attachment>()
                        .ListTyped(attachmentID: context.Request.QueryString.GetFirstOrDefaultAs<int>("a"))
                        .FirstOrDefault();

                var boardID = context.Request.QueryString.GetFirstOrDefault("b") != null
                                  ? context.Request.QueryString.GetFirstOrDefaultAs<int>("b")
                                  : YafContext.Current.BoardSettings.BoardID;

                if (!this.CheckAccessRights(boardID, attachment.MessageID))
                {
                    // tear it down
                    // no permission to download
                    context.Response.Write(
                        "You have insufficient rights to download this resource. Contact forum administrator for further details.");
                    return;
                }

                byte[] data;

                if (attachment.FileData == null)
                {
                    var uploadFolder = YafBoardFolders.Current.Uploads;

                    var oldFileName =
                        context.Server.MapPath(
                            "{0}/{1}.{2}".FormatWith(
                                uploadFolder,
                                attachment.MessageID > 0
                                    ? attachment.MessageID.ToString()
                                    : "u{0}".FormatWith(attachment.UserID),
                                attachment.FileName));

                    var newFileName =
                        context.Server.MapPath(
                            "{0}/{1}.{2}.yafupload".FormatWith(
                                uploadFolder,
                                attachment.MessageID > 0
                                    ? attachment.MessageID.ToString()
                                    : "u{0}".FormatWith(attachment.UserID),
                                attachment.FileName));

                    // use the new fileName (with extension) if it exists...
                    var fileName = File.Exists(newFileName) ? newFileName : oldFileName;

                    using (var input = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        data = input.ToArray();
                        input.Close();
                    }
                }
                else
                {
                    data = attachment.FileData;
                }

                context.Response.ContentType = attachment.ContentType;
                context.Response.AppendHeader(
                    "Content-Disposition",
                    "attachment; filename={0}".FormatWith(
                        HttpUtility.UrlPathEncode(attachment.FileName).Replace("+", "_")));
                context.Response.OutputStream.Write(data, 0, data.Length);
                this.GetRepository<Attachment>()
                    .IncrementDownloadCounter(context.Request.QueryString.GetFirstOrDefaultAs<int>("a"));
            }
            catch (Exception x)
            {
                this.Get<ILogger>()
                    .Log(
                        YafContext.Current.PageUserID,
                        this,
                        "URL: {0}<br />Referer URL: {1}<br />Exception: {2}".FormatWith(
                            context.Request.Url,
                            context.Request.UrlReferrer != null ? context.Request.UrlReferrer.AbsoluteUri : string.Empty,
                            x),
                        EventLogTypes.Information);
                context.Response.Write(
                    "Error: Resource has been moved or is unavailable. Please contact the forum admin.");
            }
        }
开发者ID:bugjohnyang,项目名称:YAFNET,代码行数:91,代码来源:YafResourceHandler.cs


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