private static bool WriteFileDownload(HttpResponseBase response, string filePath, string fileName, Encoding encoding)
        {
            try
            {
                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Length >= MAXSIZE)
                {
                    throw new Exception(SizeOver2GException);
                }

                return(ExecuteDownload(response, () =>
                {
                    response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    response.AddHeader("Content-Transfer-Encoding", "binary");

                    response.WriteFile(fileInfo.FullName);
                }, true, filePath, fileName, encoding));
            }
            catch
            {
                if (response != null)
                {
                    response.Close();
                }
                throw;
            }
        }
Ejemplo n.º 2
0
        public void ProcessRequest(HttpContext context)
        {
            HttpContextWrapper baseCtx = new HttpContextWrapper(context);

            // Set the content type
            // response.ContentType = setting.ContentType;
            HttpResponseBase response = baseCtx.Response;

            // Compress
            baseCtx.CompressResponse();

            // Write
            Uri    requestUrl         = baseCtx.Request.Url;
            string assetFilePath      = requestUrl.AbsolutePath;
            string serverSideFilePath = baseCtx.Server.MapPath(assetFilePath);
            string contentType;// = "text/plain";

            switch (Path.GetExtension(serverSideFilePath))
            {
            case ".js": contentType = "application/x-javascript"; break;

            case ".css": contentType = "text/css"; break;

            default:
                contentType = "text/plain"; break;
            }
            response.ContentType = contentType;
            response.WriteFile(serverSideFilePath);

            //File.ReadAllText(
            //using (StreamWriter sw = new StreamWriter(response.OutputStream))
            //{
            //    sw.Write(asset.Content);
            //}
        }
 protected override void WriteFile(HttpResponseBase response)
 {
     response.ContentType = _contentType;
     response.WriteFile(_fileName);
     response.Flush();
     File.Delete(_fileName);
 }
Ejemplo n.º 4
0
        public static void ExportXLS(HttpResponseBase response, string myPageName, List <ColumnInfo> columns, DataSet ds)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"\Excel\" + myPageName + ".xls";

            response.Clear();
            response.Buffer  = true;
            response.Charset = "utf-8";
            response.AppendHeader("Content-Disposition", "attachment;filename=" + myPageName + ".xls");
            response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            response.ContentType     = "application/ms-excel";

            ExcelWriter excel = new ExcelWriter(path);

            try
            {
                excel.BeginWrite();

                short row = 0;

                for (short k = 0; k < columns.Count; k++)
                {
                    excel.WriteString(row, k, columns[k].ColumnDisplayName);
                }

                DataTable dt = ds.Tables[0];

                for (short i = 0; i < dt.Rows.Count; i++)
                {
                    row++;
                    for (short j = 0; j < columns.Count; j++)
                    {
                        ColumnInfo column     = columns[j];
                        string     columnType = column.ColumnType;
                        string     columnName = column.ColumnName;
                        object     value      = ds.Tables[0].Rows[i][columnName];

                        if (columnType != null && columnType.Equals("date"))
                        {
                            value = value.ToString().Split(new char[] { ' ' }, StringSplitOptions.None)[0];
                        }
                        excel.WriteString(row, j, value.ToString());
                    }
                }
            }
            finally
            {
                excel.EndWrite();
            }

            FileInfo file = new FileInfo(path);

            if (file.Exists)
            {
                response.WriteFile(path);
                response.Flush();
                file.Delete();
            }
        }
Ejemplo n.º 5
0
        public void WriteFile(string contentType, string localFilePath, string displayName)
        {
            _response.ContentType = contentType;

            if (displayName != null)
            {
                _response.AppendHeader("content-disposition", "attachment; filename=" + displayName);
            }

            _response.WriteFile(localFilePath);
        }
Ejemplo n.º 6
0
        private void setResponseInfo()
        {
            doc.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "Content\\Templates\\" + fileName);
            Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.WriteFile(AppDomain.CurrentDomain.BaseDirectory + "Content\\Templates\\" + fileName);
            Response.End();
            FileInfo fileInfo = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "Content\\Templates\\" + fileName);

            fileInfo.Delete();
        }
Ejemplo n.º 7
0
 private static void OutputFile(HttpResponseBase response, System.IO.FileInfo fileInfo, byte[] content)
 {
     if (content != null)
     {
         response.BinaryWrite(content);
     }
     else
     {
         response.WriteFile(fileInfo.FullName, 0, fileInfo.Length);
     }
     response.Flush();
     response.End();
 }
Ejemplo n.º 8
0
            /// <summary>
            /// 导出
            /// </summary>
            public void DoExport()
            {
                if (this.FileName == null)
                {
                    this.FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                }

                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.{1}", FileName, FileExt));

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                ExcleExporter.Book.Write(ms);
                if (FileExt.Substring(0, 1) == ".")
                {
                    FileExt = FileExt.Substring(1);
                }
                if (FileExt.ToLower() == "zip")
                {
                    var path = Server.MapPath("~/Exports/");
                    Utils.CreateFolderIfNeeded(path);

                    if (System.IO.File.Exists(path + FileName + ".xls"))
                    {
                        System.IO.File.Delete(path + FileName + ".xls");
                    }

                    using (var fileStream = new System.IO.FileStream(path + FileName + ".xls",
                                                                     System.IO.FileMode.CreateNew, System.IO.FileAccess.ReadWrite))
                    {
                        ms.Position = 0;
                        ms.CopyTo(fileStream); // fileStream is not populated
                    }

                    this.Files.Add(path + FileName + ".xls");

                    if (System.IO.File.Exists(path + FileName + ".zip"))
                    {
                        System.IO.File.Delete(path + FileName + ".zip");
                    }
                    ZipHelper.Zip(this.Files.ToArray(), path + FileName + ".zip");

                    Response.WriteFile(path + FileName + ".zip");
                }
                else
                {
                    Response.BinaryWrite(ms.ToArray());
                }
                ExcleExporter.Book = null;
                ms.Close();
                ms.Dispose();
            }
Ejemplo n.º 9
0
        public static void DownloadFile(string filePath, HttpResponseBase response)
        {
            var fullPath = FileHelper.GetFullPath(filePath);
            var isExist  = File.Exists(fullPath);

            if (isExist)
            {
                response.Clear();
                response.ContentType     = "application/octet-stream";
                response.ContentEncoding = Encoding.UTF8;
                response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(Path.GetFileName(filePath), Encoding.UTF8));
                // Response.AppendHeader("Content-Length", fInfo.Length.ToString());
                response.WriteFile(filePath);
            }
        }
Ejemplo n.º 10
0
        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase response = context.HttpContext.Response;
            HttpRequestBase  request  = context.HttpContext.Request;

            if (!HttpCacheHelper.IsFileFromCache(File, request, response))
            {
                string fileName;
                string fileNameEncoded = HttpUtility.UrlEncode(File.Name);

                if (context.HttpContext.Request.UserAgent.Contains("MSIE")) // IE < 9 do not support RFC 6266 (RFC 2231/RFC 5987)
                {
                    fileName = "filename=\"" + fileNameEncoded + "\"";
                }
                else
                {
                    fileName = "filename*=UTF-8\'\'" + fileNameEncoded; // RFC 6266 (RFC 2231/RFC 5987)
                }
                string mime;
                string disposition;
                if (IsDownload)
                {
                    mime        = "application/octet-stream";
                    disposition = "attachment; " + fileName;
                }
                else
                {
                    mime        = Helper.GetMimeType(File);
                    disposition = (mime.Contains("image") || mime.Contains("text") || mime == "application/x-shockwave-flash" ? "inline; " : "attachment; ") + fileName;
                }

                response.ContentType = mime;
                response.AppendHeader("Content-Disposition", disposition);
                response.AppendHeader("Content-Location", File.Name);
                response.AppendHeader("Content-Transfer-Encoding", "binary");
                response.AppendHeader("Content-Length", File.Length.ToString());
                response.WriteFile(File.FullName);
                response.End();
                response.Flush();
            }
            else
            {
                response.ContentType = IsDownload ? "application/octet-stream" : Helper.GetMimeType(File);
                response.End();
            }
        }
Ejemplo n.º 11
0
        void SendFile(string fullPath, string extension)
        {
            var eTag = GetETag(fullPath);

            response.Cache.SetCacheability(HttpCacheability.Public);
            response.Cache.SetExpires(DateTime.Now.AddYears(1));
            response.Cache.SetETag(eTag);

            var requestETag = request.Headers["If-None-Match"];

            if (requestETag == eTag)
            {
                NotModified();
                return;
            }

            SetResponseContentType(fullPath, extension);
            response.WriteFile(fullPath);
        }
Ejemplo n.º 12
0
 public void WriteFile(string file)
 {
     _response.WriteFile(file);
 }
 public override void WriteFile(string filename)
 {
     _httpResponseBase.WriteFile(filename);
 }
 public override void WriteFile(IntPtr fileHandle, long offset, long size)
 {
     proxiedResponse.WriteFile(fileHandle, offset, size);
 }