0
votes

I've written a custom httphandler for DocX files, and I'm trying to have the files be displayed through an iframe.

Here's my controller:

    public ActionResult LoadDOC(string path)
    {
        var fsSource = new FileStream(path, FileMode.Open, FileAccess.Read);
        return new FileStreamResult(fsSource, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
        {
            FileDownloadName = "newfile.docx"
        };
    }

}

The file name needs to be changed so that the httphandler can pick it up. The files reside on the fileserver without extensions (they're renamed to a guid). For example:

"\\\\fileservername\\Documents\\811943a3-56f7-42cb-8450-1b8319a426b4\\633d9f3e-df99-408e-b59c-ec8efa4fa41f"

I can't change the way the files reside on the server, I'll have to add the extension through code.

When the above is executed in an iframe, the file is immediately downloaded. PDF files and text files render properly however.

Here is the custom httphandler:

      <add name="DOCXhandler" path="*.docx" verb="GET" type="MyProject.Handlers.DocxHandler, MyProject" preCondition="integratedMode" />

How can I go about either changing the file stream extension using this approach, or another approach to perhaps achieve the desired result of the file displayed in an iframe?

Is it possible to just change the handler by content type?

Edit: Question for clarity;

  • Is it possible to make an http handler work by content type, instead of file extension?
  • How can I make sure the filestreamresult uses my custom httphandler for .docx files when displaying in the iframe?
  • Am I going about this the correct way, or am I missing something?
2

2 Answers

0
votes

Consider adding ".docx" extension to rendered Url to the file and than removing it your LoadDoc action (i.e. cheap hack - path = path.Replace(".docx", ""), prefer using methods from Path class to do that manipulation).

Side note: exposing server side file path is generally bad idea from security point of view.

0
votes

I actually was going about the issue very poorly.

I changed the way the controller set the iframe to an actual dummy file path: for example: "localhost/Project/12345_6789.docx" where 12345 is the group identifier, and 6789 is the document identifier.

The httphandler I made expects these, and changes the file path to: "////fileserverpath/documentspath/12345/6789"

I'm no longer streaming the file directly through the controller, but instead through my handler for the request.

Hope this helps someone out there!