コード例 #1
0
        public object Get(DownloadAttachment request)
        {
            bool   useAttachmentsRelativePath  = false;
            string sUseAttachmentsRelativePath = AppSettings.Get <string>("UseAttachmentsRelativePath");

            if (!string.IsNullOrWhiteSpace(sUseAttachmentsRelativePath) && bool.TryParse(sUseAttachmentsRelativePath, out bool bUseAttachmentsRelativePath))
            {
                useAttachmentsRelativePath = bUseAttachmentsRelativePath;
            }

            string strDirectory        = request.Directory;
            string strFileName         = request.FileName;
            string appSettingsFolder   = request.AttachmentKind;
            string baseAttachmentsPath = AppSettings.Get <string>(appSettingsFolder);

            string filePath;

            if (useAttachmentsRelativePath)
            {
                filePath = "~/" + baseAttachmentsPath + strDirectory + "/" + strFileName.MapHostAbsolutePath();
            }
            else
            {
                filePath = baseAttachmentsPath + strDirectory + "\\" + strFileName;
            }

            var file = new FileInfo(filePath);

            Response.StatusCode = 200;
            Response.AddHeader("Content-Transfer-Encoding", "binary");
            Response.AddHeader("Content-Disposition", "attachment; filename=\"{0}\"".Fmt(file.Name));

            return(new HttpResult(file, true));
        }
コード例 #2
0
ファイル: AttachmentService.cs プロジェクト: JFigue27/MDC
        public object Get(DownloadAttachment request)
        {
            bool   useAttachmentsRelativePath  = false;
            string sUseAttachmentsRelativePath = AppSettings.Get <string>("UseAttachmentsRelativePath");

            if (!string.IsNullOrWhiteSpace(sUseAttachmentsRelativePath) && bool.TryParse(sUseAttachmentsRelativePath, out bool bUseAttachmentsRelativePath))
            {
                useAttachmentsRelativePath = bUseAttachmentsRelativePath;
            }

            string strDirectory        = request.Directory;
            string strFileName         = request.FileName;
            string appSettingsFolder   = request.AttachmentKind;
            string baseAttachmentsPath = AppSettings.Get <string>(appSettingsFolder);

            string filePath;

            if (useAttachmentsRelativePath)
            {
                filePath = "~/" + baseAttachmentsPath + strDirectory + "/" + strFileName.MapHostAbsolutePath();
            }
            else
            {
                filePath = baseAttachmentsPath + strDirectory + "\\" + strFileName;
            }

            var file = new FileInfo(filePath);

            Log.Info($"Attachment Download: [{filePath}] by User: [{GetSession().UserName}]");

            //Response.StatusCode = 200;
            //Response.AddHeader("Content-Type", "application/octet-stream");
            //Response.AddHeader("Content-Disposition", $"attachment;filename=\"{file.Name}\"");

            return(new HttpResult(file, true)
            {
                //ContentType = "application/octet-stream",
                Headers =
                {
                    [HttpHeaders.ContentDisposition] = $"inline; filename=\"{file.Name}\""
                }
            });
        }