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


C# FileStream.GetBytesAsync方法代码示例

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


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

示例1: Post

        public async Task<HttpResponseMessage> Post(string fileName, string fileExtension, string contentType)
        {
            try
            {
                Check.IsNotEmptyOrWhiteSpace(fileName, "fileName");
                Check.IsNotEmptyOrWhiteSpace(fileExtension, "fileExtension");
                Check.IsNotEmptyOrWhiteSpace(contentType, "contentType");

                byte[] fileContent = default(byte[]);
                if (Request.Content.IsMimeMultipartContent("form-data"))
                {
                    string root = Path.GetTempPath();
                    MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(root);
                    await Request.Content.ReadAsMultipartAsync(streamProvider);
                    MultipartFileData multipartFileData = streamProvider.FileData.First();

                    /*********************************************************
                     * This API under stress can cause IO exception, for File read
                     * or Delete operation. More details @https://aspnetwebstack.codeplex.com/workitem/176 
                     * Following logic retries file operations at most 3 times
                     * inducing the recommended delay in case of failure. 
                     */ 
                    int executionDelay = 50, 
                        readRetryCount = 3,
                        deleteRetryCount = 3;

                    // Retry mechanics for file read operation
                    while (readRetryCount-- > 0) 
                    {
                        try
                        {
                            using (FileStream fileSource = new FileStream(multipartFileData.LocalFileName, FileMode.Open, FileAccess.Read))
                            {
                                fileContent = await fileSource.GetBytesAsync();
                            }
                            break;
                        }
                        catch (IOException ioe)
                        {
                            diagnostics.WriteErrorTrace(TraceEventId.Exception, ioe.ToString());
                        }
                        await Task.Delay(executionDelay);
                    }

                    // Retry mechanics for file delete operation
                    while (deleteRetryCount-- > 0)
                    {
                        try
                        {
                            await Task.Factory.StartNew(() => System.IO.File.Delete(multipartFileData.LocalFileName));
                            break;
                        }
                        catch (IOException ioe)
                        {
                            diagnostics.WriteErrorTrace(TraceEventId.Exception, ioe.ToString());
                        }
                        await Task.Delay(executionDelay);
                    }

                }
                else
                {
                    var bufferlessInputStream = HttpContext.Current.Request.GetBufferlessInputStream();
                    Check.IsNotNull(bufferlessInputStream, "HttpContext.Current.Request.GetBufferlessInputStream()");
                    fileContent = await bufferlessInputStream.GetBytesAsync();
                }

                this.fileService.ValidateForUpload(fileName, this.user.UserId);

                DataFile dataFile = new DataFile
                {
                    FileContent = fileContent,
                    ContentType = contentType,
                    FileName = fileName,
                    CreatedBy = this.user.UserId,
                    FileExtentsion = fileExtension
                };

                IFileHandler fileHandler = this.fileHandlerFactory.GetFileHandler(contentType, this.user.UserId);
                diagnostics.WriteInformationTrace(Utilities.Enums.TraceEventId.Flow, "blob-before file upload");
                var uploadedDataDetails = fileHandler.Upload(dataFile);
                diagnostics.WriteInformationTrace(Utilities.Enums.TraceEventId.Flow, "blob-after file upload");
                if (!uploadedDataDetails.Any() || uploadedDataDetails.Any(dd => dd.FileDetail.FileId == 0))
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, MessageStrings.UploadFailed);
                }

                return Request.CreateResponse<IEnumerable<DataDetail>>(HttpStatusCode.OK, uploadedDataDetails);
            }
            catch (ValidationException validationException)
            {
                HttpError error = validationException.GetHttpError(string.Format(MessageStrings.Upload_Validation_Error_Template, validationException.FileName));
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
            }
            catch (ArgumentException ex)
            {
                message = string.Format(CultureInfo.CurrentCulture, MessageStrings.Argument_Error_Message_Template, ex.ParamName);
                status = HttpStatusCode.BadRequest;
            }

//.........这里部分代码省略.........
开发者ID:CDLUC3,项目名称:dataup2,代码行数:101,代码来源:BlobController.cs


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