问题
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?!?
回答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:
- try generating your file to the application folder structure
- 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>
来源:https://stackoverflow.com/questions/31441392/how-can-i-get-a-link-to-a-file-to-open-said-file