user482375
user482375

Reputation:

File Not Found when C# added to Custom Application Page Code Behind

I have a Custom Application Page in SharePoint 2010. Everything works fine until I put in a certain piece of C# code in the code behind. When I add:

Document document = new Document();
document.LoadFromFile(@"C:\TestFolder\MYDOC.DOC");

document.SaveToFile(@"C:\Files\MYDOC.PDF", FileFormat.PDF);

When I add this code I get this error:

File Not Found

This Custom Application Page uses a third party PDF dll called Spire.Doc. That DLL is added in the Package and no error is presented even when the DLL is in the package when the code above is not present. The error ONLY happens when the above code is in the code behind of the Custom Application Page. Any ideas? I am completely stuck.

Thanks!

Upvotes: 0

Views: 517

Answers (2)

From my experience, the file not found page indicates that is no custom error page found there.

Like what Oleg Savelyev has pointed out it may be an issue with permissions to read/write to the local disk.

It would probably a good idea to use Memorystream to show or save the PDF document.

HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=MYDOC.PDF");
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.WriteFile();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();

Used this to save html to PDF. Could work with .doc files aswell. If not look into http://www.aspose.com/.net/word-component.aspx for document to PDF.

Hope this is helpful.

Upvotes: 0

Oleg Savelyev
Oleg Savelyev

Reputation: 968

There are may be a lot of reasons leading to this error.

At first try to look into SharePoint logs where you can find more extended diagnostic info with stacktrace. It will allow you to be sure that error is caused by this particular piece of code.

Second: be sure that your document is really present on disk.

And third: SharePoint is a complex system with high barrier to entry to its permissions and roles. You (your account who uses your application page) should have proper permissions to work with files on disk. Check the context for application page - for example when account is admin in one subsite http://root/sites/subsite and navigates to application page with something like http://root/_layouts/apppage he may not be an admin of root site. Anyway you could try to call your code with elevated privileges. Something like

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    Document document = new Document();
    document.LoadFromFile(@"C:\TestFolder\MYDOC.DOC");
    document.SaveToFile(@"C:\Files\MYDOC.PDF", FileFormat.PDF);
});

Upvotes: 1

Related Questions