I generate a PDF file within a Sharepoint page; as I have so far been unable to let the user choose a landing spot for the pdf file (as asked about here), I have decided to create a link to the file on the page, hoping that clicking the link will open the file. Here's the code:
String pdfFileID = GetYYYYMMDDAndUserNameAndAmount();
String pdfFileName = String.Format("DirectPayDynamic_{0}.pdf",
pdfFileID);
String fileFullpath
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), pdfFileName);
String fileLinkBase = "Generated PDF: <a href=\"file:///{0}\">{1}</a>";
String filelink = String.Format(fileLinkBase, fileFullpath, pdfFileName);
File.WriteAllBytes(fileFullpath, bytes);
AddVerticalSpace();
var pdflink = new Label // would this be preferable: "LiteralControl pdflink = new LiteralControl();" ?
{
CssClass = "dplatypus-webform-field-label",
Text = filelink
};
this.Controls.Add(pdflink);
This creates the link just fine:
...but clicking the link doesn't open the file. Or give an err msg; it just sits there like a lump in a bog.
To verify that it wasn't something to do with the PDF file, I tried creating a text file in the path and accessing it:
String pdfFileName = String.Format("DirectPayDynamic_{0}.pdf", pdfFileID);
pdfFileName = "czechitout.txt"; // Remove after testing
...but alas, also clicking on "czechitout.txt" does nothing - Notepad does not open or give any indication at all as to the reason for its recalcitrance to do so.
So: how can I get a link on a Sharepoint page to invoke the associated software to run the file referenced?
UPDATE
NOTE: I would settle for opening Explorer, displaying the file (usually the user will be copying it from one place to another anyway).
UPDATE 2
By simply saving the file without any path info, like so:
String pdfFileID = GetYYYYMMDDAndUserNameAndAmount();
String pdfFileName = String.Format("DirectPayDynamic_{0}.pdf", pdfFileID);
String fileLinkBase = "Generated PDF: <a href=\"file:///{0}\">{1}</a>";
String filelink = String.Format(fileLinkBase, pdfFileName, pdfFileName);
File.WriteAllBytes(fileFullpath, bytes);
AddVerticalSpace();
var pdflink = new Label // would this be preferable: "LiteralControl pdflink = new LiteralControl();" ?
{
CssClass = "finaff-webform-field-label",
Text = filelink
};
this.Controls.Add(pdflink);
When I mash the link, I get, "Error on Page"
F12ing gives me the low-down/skinny/buttlescut:
Not allowed to load local resource: file:///DirectPayDynamic_20150717_clayshan_0.pdf
UPDATE 3
I then tried the suggestion of saving to a specific directory (without using the fancy-pants "SpecialFolder" jazz, like so:
String pdfFileName = String.Format(@"C:\Projects\DirectPaymentWebForm\DirectPayDynamic_{0}.pdf", pdfFileID);
...but I fared no better that way (the file is plopped there, but trying to invoke it via the link is an exercise in futility).
UPDATE 4
I even tried it with a lowly text file, de modo que:
String pdfFileName = @"C:\Projects\DirectPaymentWebForm\tryopeningthis.txt";
...but my catch block is reached:
catch (Exception ex)
{
String exMsg = ex.Message;
}
...wherein the msg is, "Access to the path 'C:\Projects\DirectPaymentWebForm\tryopeningthis.txt' is denied."
The path/file does exist; why would I not get that same err msg with the pdf file?!?