1

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:

enter image description here

...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?!?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

1

This is not the problem with the file. It is because of the security concepts. Any file that we are referring inside our web pages, need to be under the same directory structure. If you could open the developer tools using F12 option inside the browser, you can see some console errors saying

Not allowed to load local resource: "your file path"

you can achieve that in two ways:

  1. try generating your file to the application folder structure
  2. create a link button and on click of that button, send the document as a stream to output

Hope this solves your problem.

EDIT with the approach mentioned in point 1, you can use below code.

   <a href="<your file path>">link</a>

In the file path, it is not required to mention file:/// text. This works

EDIT The above process makes the file to load in the current page itself.

To load file in another window or as a popup, use

<a href="javascript:window.Open("<file name>");">link</a>
Mallikarjuna
  • 130
  • 9
  • I don't know what you mean by #1, as users will access this via an URL - what would be the "application folder structure" for them? In #2, do you mean output the pdf on a new page within the browser? If so, that won't work - we need it to be a standalone .pdf file residing on disk. – B. Clay Shannon-B. Crow Raven Jul 16 '15 at 15:46
  • 1
    1) I meant "application folder structure" to the directory of your sharepoint site. If your site directory path is "C:\\Sites\", then your PDF should be under the above said directory or its child directories. 2) one more way is to have another page with an IFrame in it and load the PDF into that frame, so that the user can have option to save – Mallikarjuna Jul 16 '15 at 15:54
  • Did this answer help you?? – Mallikarjuna Jul 17 '15 at 04:05
  • Not really; I need to create a "regular" PDF that the user can easily open, or at least its location in Explorer, so they can copy the file and paste it elsewhere. – B. Clay Shannon-B. Crow Raven Jul 17 '15 at 17:10
  • 1
    in your code, you are creating **fileFullPath** by mentioning desktop path. Instead, you can just mention the file name alone or any subdirectory w.r.t your sharepoint page. by doing like that, you can make your link opening the PDF file. – Mallikarjuna Jul 17 '15 at 18:31
  • Thanks, but see Update 2. – B. Clay Shannon-B. Crow Raven Jul 17 '15 at 20:41
  • 1
    please check my edit. your Update 2 will work. Only thing is you have to remove the file:/// text. The href should be just relative path of your file. – Mallikarjuna Jul 18 '15 at 05:49
  • It would be nice if that were true, but removing the "file:///" still fails, tells me "Error on Page", and F12ing shows me, "Not allowed to load local resource: file:///C:/Users/TEMP.SP.016/Desktop/DirectPayDynamic_20150720_clayshan_0.pdf" – B. Clay Shannon-B. Crow Raven Jul 20 '15 at 15:40