Click here to Skip to main content
15,886,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to write custom http handler (Pdf File Handler) for an existing application. 
File is located in shared folder in different server. 

I wrote customPdfFileHandler and place the dll to the bin dir of my webApplicaion. 

 
 Code in customPdfFileHandler is : 
 <pre lang="c#">public void ProcessRequest(HttpContext context)
        {

            dynamic emp = context.Session["EmpID"];
            if (emp == null || string.IsNullOrEmpty(emp))
            {
                HttpContext.Current.Response.StatusCode = 404;
                HttpContext.Current.Response.Flush();

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ClearHeaders();
                context.Response.ContentType =  "application/pdf";
                context.Response.AddHeader("Content-Disposition", "attachment");
                context.Response.TransmitFile(context.Request.RawUrl);
                context.Response.Flush();
                context.Response.Clear();
                context.ApplicationInstance.CompleteRequest();
            }
		}


But this throw error :


Requested URL
http://localhost:55558/ip-myserver/d$/Websites/mysites.net/pdfFiles/archive/Example.pdf

Physical Path
C:\Users\jdoe\Desktop\DemoFolder\DemoWeb\DemoWeb\ip-myserver\d$\Websites\mysites.net\pdfFiles\archive\Example.pdf

file path are given in web.config files of web application.

What I have tried:

public void ProcessRequest(HttpContext context)
        {

            dynamic emp = context.Session["EmpID"];
            if (emp == null || string.IsNullOrEmpty(emp))
            {
                HttpContext.Current.Response.StatusCode = 404;
                HttpContext.Current.Response.Flush();

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ClearHeaders();
                context.Response.ContentType =  "application/pdf";
                context.Response.AddHeader("Content-Disposition", "attachment");
                context.Response.TransmitFile(context.Request.RawUrl);
                context.Response.Flush();
                context.Response.Clear();
                context.ApplicationInstance.CompleteRequest();
            }
		}
Posted
Updated 15-Nov-17 5:53am

1 solution

The raw URL is defined as the part of the URL following the domain information. In the URL string http://www.contoso.com/articles/recent.aspx, the raw URL is /articles/recent.aspx. The raw URL includes the query string, if present.

So given your URL of:
http://localhost:55558/ip-myserver/d$/Websites/mysites.net/pdfFiles/archive/Example.pdf

the RawUrl will be:
/ip-myserver/d$/Websites/mysites.net/pdfFiles/archive/Example.pdf

This will be resolved relative to the current path. So if your application is on drive C:, it will be looking for a file located in:
C:\ip-myserver\d$\Websites\mysites.net\pdfFiles\archive\Example.pdf

It looks like you're trying to load the file from a UNC path, which should be:
\\ip-myserver\d$\Websites\mysites.net\pdfFiles\archive\Example.pdf

Try something like this:
C#
public void ProcessRequest(HttpContext context)
{
    dynamic emp = context.Session["EmpID"];
    if (emp == null || string.IsNullOrEmpty(emp))
    {
        HttpContext.Current.Response.StatusCode = 404;
        HttpContext.Current.Response.Flush();
        return;
    }
    
    Uri path;
    if (!Uri.TryCreate("file:/" + context.Request.RawUrl, UriKind.Absolute, out path))
    {
        HttpContext.Current.Response.StatusCode = 404;
        HttpContext.Current.Response.Flush();
        return;
    }
    
    context.Response.ClearContent();
    context.Response.ClearHeaders();
    context.Response.ContentType =  "application/pdf";
    context.Response.AddHeader("Content-Disposition", "attachment");
    context.Response.TransmitFile(path.LocalPath);
    context.Response.Flush();
    context.Response.Clear();
    context.ApplicationInstance.CompleteRequest();
}

NB: You'll need to make sure that your application pool is running as a user which has permission to read files from the UNC path. You'll probably need to avoid using the default admin shares (d$), and switch to using specific folder shares instead.
 
Share this answer
 
Comments
s23user 15-Nov-17 14:03pm    
I do have access to the path \\networkShared\SHARED\john\ApplicationReview\ExeAppSchedule1.pdf

Requested URL
http://localhost:8080/networkShared/SHARED/john/ApplicationReview/ExeAppSchedule1.pdf

Physical Path
C:\TestPublish\TestLib\networkShared\SHARED\john\ApplicationReview\ExeAppSchedule1.pdf


this is how my web config looks like:

<configuration>
<appsettings>
<add key="pdfFile" value="\\networkShared\SHARED\john\ApplicationReview\ExeAppSchedule1.pdf " />

<system.web>
<compilation targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<identity impersonate="true" />

<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />


<system.webserver>
<directoryBrowse enabled="true" />
<handlers>
<add name="PdfFile" path="*.pdf" verb="*" type="CustomFileHandler.CustomFileHandler" resourceType="File" preCondition="integratedMode" />
<add name="JpgFile" path="*.jpg" verb="*" type="CustomFileHandler.CustomFileHandler" resourceType="File" preCondition="integratedMode" />


F-ES Sitecore 16-Nov-17 5:50am    
You might have access to that file but does the account your code is running under? Unless you've changed the user in IIS it won't. If I was you I'd download the file over http (use WebClient or similar) and then send that to the client rather than trying to access the file system directly. Access the file system of other machine via asp.net is way more hassle if you can access the file over http.
s23user 16-Nov-17 9:30am    
How do I do download the file over http, could you please walk me through it?
F-ES Sitecore 16-Nov-17 10:00am    
s23user 17-Nov-17 13:23pm    
I checked that link but I did not understand how to and what do I pass as an url when creating web request as the file I am trying to download is in file server but not webserver.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900