This project is read-only.

V1.2 ImageHandler problems with PDF files

Topics: Developer Forum, User Forum
May 7, 2008 at 1:28 PM
When linking a PDF document in an HTML section I have been unable to use the imagehandler to display PDF files. I can browse to the server, and add the file and it shows up in appdata just fine. However when I then go back to view mode (or as an anon user on the site) and click on the new link to the PDF doc, I get a blank window with the following line in the address: http://www.tiptonumc.org/ImageHandler.ashx?UploadedFile=true&pg=b7dc5b56-9530-421e-88e4-6ddfbd73add3&image=~/AppData/UserImages/File/Documents/Testdoc.pdf
Bug or user error?
Imagehandler appears to have no problems with images.
May 13, 2008 at 9:46 AM
Edited May 17, 2008 at 10:11 PM

I have modified source code in:
App_Code/FCKEditor/FileBrowserConnector.cs
--------------------------------------------------------------------------------
private string GetUrlFromPath(string resourceType, string folderPath)
{
if (resourceType == null || resourceType.Length == 0)
return this.UserFilesPath.TrimEnd('/') + folderPath;
else
{
//pageId is used to enforce a secure download of the image with authentication of the user 
// [XXX] will be replaced by the pageId when saving the data in HtmlContent.ascx.cs 
string pageId = "[XXX]";
if (resourceType == "File")
return ResolveUrl("~/FileHandler.ashx?UploadedFile=true&pg=" + pageId + "&file=" + this.UserFilesPath + resourceType + folderPath);
else
{
if (resourceType == "Image")
return ResolveUrl("~/ImageHandler.ashx?UploadedFile=true&pg=" + pageId + "&image=" + this.UserFilesPath + resourceType + folderPath);
else 
return ResolveUrl(this.UserFilesPath + resourceType + folderPath);
}
}
}
--------------------------------------------------------------------------------
And I have created new file in the root
FileHandler.ashx
--------------------------------------------------------------------------------
<%
@ WebHandler Language="C#" Class="FileHandler" %> 

//===============================================================================================
//
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
//
//===============================================================================================
using System;
using System.Web;
using System.IO;
using MyWebPagesStarterKit;
/// <summary> ///
This handler serves files from the FCKEDITOR User File folder.
/// </summary>
public class FileHandler : IHttpHandler 
{
#region
IHttpHandler Members
public bool IsReusable
{
 get { return true; }
}
 public void ProcessRequest(HttpContext context)
{
try
{
string file = context.Request.QueryString["file"];
//build the path 
string path = context.Request.MapPath(file);
if (File.Exists(path))
{
context.Response.Clear();
context.Response.AddHeader("Pragma", "public");
context.Response.AddHeader("Expires", "0");
context.Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
context.Response.AddHeader("Content-Type", "application/force-download");
context.Response.AddHeader("Content-Type", "application/octet-stream");
context.Response.AddHeader("Content-Type", "application/download");
context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", new FileInfo(path).Name.ToString()));
context.Response.AddHeader("Content-Transfer-Encoding", "binary");
context.Response.AddHeader("Content-Length", new FileInfo(path).Length.ToString());
context.Response.WriteFile(path);
}
}
catch
{
context.Response.Clear();
context.Response.End();
}
}
#endregion
}

May 14, 2008 at 2:01 AM


ricap wrote:
I have modified source code in:

App_Code/FCKEditor/FileBrowserConnector.cs

Thanks!  I'll give it a try.

May 17, 2008 at 2:16 PM
Edited May 17, 2008 at 2:49 PM

That did the trick!  Thanks.

One word of caution, however, to anyone attempting to cut&paste to make this work.  There are many places in the filehandler.ashx code posted above where line splits prevent it from working correctly - Looks like posting it in the forum screws up the formatting.

Correct those and it works perfectly!

ricap wrote:



And I have created new file in the root

FileHandler.ashx


 

May 17, 2008 at 10:16 PM
Ok, I have modified my first formatting message (now is more compact).