private void DownloadFile()
        {
            if (
                (CurrentPage != null) &&
                (sharedFile != null)
                )
            {
                string downloadPath = Page.Server.MapPath(WebUtils.GetApplicationRoot()
                                                          + "/Data/Sites/" + this.CurrentPage.SiteId.ToString(CultureInfo.InvariantCulture) + "/SharedFiles/")
                                      + sharedFile.ServerFileName;

                if (File.Exists(downloadPath))
                {
                    FileInfo fileInfo = new System.IO.FileInfo(downloadPath);
                    Page.Response.AppendHeader("Content-Length", fileInfo.Length.ToString(CultureInfo.InvariantCulture));
                }
                else
                {
                    log.Error("Shared File Not Found. User tried to download file " + downloadPath);
                    return;
                }

                string fileType = Path.GetExtension(sharedFile.FriendlyName).Replace(".", string.Empty);

                string mimeType = SiteUtils.GetMimeType(fileType);
                Page.Response.ContentType = mimeType;

                if (SiteUtils.IsNonAttacmentFileType(fileType))
                {
                    //this will display the pdf right in the browser
                    Page.Response.AddHeader("Content-Disposition", "filename=\"" + HttpUtility.UrlEncode(sharedFile.FriendlyName, Encoding.UTF8) + "\"");
                }
                else
                {
                    // other files just use file save dialog
                    Page.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(sharedFile.FriendlyName, Encoding.UTF8) + "\"");
                }

                //Page.Response.AddHeader("Content-Length", documentFile.DocumentImage.LongLength.ToString());

                try
                {
                    Page.Response.Buffer       = false;
                    Page.Response.BufferOutput = false;
                    if (Page.Response.IsClientConnected)
                    {
                        // TODO: a different solution, Response.TransmitFile has a 2 GB limit
                        Page.Response.TransmitFile(downloadPath);
                        SharedFile.IncrementDownloadCount(sharedFile.ItemId);
                    }
                    Page.Response.End();
                }
                catch (HttpException) { }
            }
        }
Beispiel #2
0
        private void DownloadFile()
        {
            if (CurrentPage != null && sharedFile != null)
            {
                string virtualPath = $"~/Data/Sites/{CurrentPage.SiteId.ToInvariantString()}/SharedFiles/{sharedFile.ServerFileName}";

                if (fileSystem.FileExists(virtualPath))
                {
                    WebFile fileInfo = fileSystem.RetrieveFile(virtualPath);

                    Page.Response.AppendHeader("Content-Length", fileInfo.Size.ToString(CultureInfo.InvariantCulture));
                }
                else
                {
                    log.Error($"Shared File Not Found. User tried to download file {virtualPath}");

                    return;
                }

                string fileType = Path.GetExtension(sharedFile.FriendlyName).Replace(".", string.Empty);

                string mimeType = SiteUtils.GetMimeType(fileType);
                //Page.Response.ContentType = mimeType;
                Page.Response.ContentType = "application/" + fileType;

                if (!SharedFilesConfiguration.TreatPdfAsAttachment && SiteUtils.IsNonAttacmentFileType(fileType))
                {
                    //this will display the pdf right in the browser
                    // and the file may be cached by the web browser
                    Page.Response.AddHeader("Content-Disposition", $"filename=\"{HttpUtility.UrlEncode(sharedFile.FriendlyName, Encoding.UTF8)}\"");

                    if (SharedFilesConfiguration.NonAttachmentDownloadExpireDays != 0)
                    {
                        Page.Response.AddHeader("Expires", DateTime.Now.AddDays(SharedFilesConfiguration.NonAttachmentDownloadExpireDays).ToUniversalTime().ToString("R"));
                    }
                }
                else
                {
                    // other files just use file save dialog
                    Page.Response.AddHeader("Content-Disposition", $"attachment; filename=\"{HttpUtility.UrlEncode(sharedFile.FriendlyName, Encoding.UTF8)}\"");

                    // 0 is the default so we should not be settings a cache header here
                    // attachments are not stored in the web browser cache
                    if (SharedFilesConfiguration.AttachmentDownloadExpireDays != 0)
                    {
                        Page.Response.AddHeader("Expires", DateTime.Now.AddDays(SharedFilesConfiguration.AttachmentDownloadExpireDays).ToUniversalTime().ToString("R"));
                    }
                }

                try
                {
                    Page.Response.Buffer       = false;
                    Page.Response.BufferOutput = false;

                    if (Page.Response.IsClientConnected)
                    {
                        using (Stream stream = fileSystem.GetAsStream(virtualPath))
                        {
                            stream.CopyTo(Page.Response.OutputStream);
                            SharedFile.IncrementDownloadCount(sharedFile.ItemId);
                        }
                        try
                        {
                            Page.Response.End();
                        }
                        catch (System.Threading.ThreadAbortException) { }
                    }
                }

                catch (HttpException) { }
            }
        }