Example #1
0
 /// <summary>
 /// 文件内容
 /// </summary>
 /// <param name="filePath"></param>
 /// <param name="contentType"></param>
 /// <param name="status"></param>
 public FileResult(string filePath, string contentType = "", HttpStatusCode status = HttpStatusCode.OK)
 {
     if (MvcApplication.IsStaticsCached)
     {
         var data = StaticResourcesCache.GetOrAdd(filePath, (k) =>
         {
             using (var fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
             {
                 var buffer  = new byte[fs.Length];
                 fs.Position = 0;
                 fs.Read(buffer, 0, buffer.Length);
                 return(buffer);
             }
         });
         this.Content = data;
     }
     else
     {
         byte[] data = null;
         using (var fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             var buffer = new byte[fs.Length];
             fs.Position = 0;
             fs.Read(buffer, 0, buffer.Length);
             data = buffer;
         }
         this.Content = data;
     }
     this.ContentEncoding = Encoding.UTF8;
     this.ContentType     = contentType;
     this.Status          = status;
 }
Example #2
0
 /// <summary>
 /// 文件内容
 /// </summary>
 /// <param name="isStaticsCached"></param>
 /// <param name="filePath"></param>
 /// <param name="contentType"></param>
 /// <param name="status"></param>
 public HttpFileResult(bool isStaticsCached, string filePath, string contentType = "", HttpStatusCode status = HttpStatusCode.OK)
 {
     if (isStaticsCached)
     {
         this.Content = StaticResourcesCache.GetOrAdd(filePath, filePath);
     }
     else
     {
         this.Content = FileHelper.Read(filePath);
     }
     this.ContentEncoding = Encoding.UTF8;
     this.ContentType     = contentType;
     this.Status          = status;
 }