Question

I want to open pdf file in new tab and visualize it.

This is my code :

SPLinkButton linkHelp = new SPLinkButton();
linkHelp.Text = "HELP...";
linkHelp.Target = "_blank";
linkHelp.Click += new EventHandler(linkHelp_Click);

This is my button event:

void linkHelp_Click(object sender, EventArgs e)
{
   string path = @"http://sf-spsdev02/TestSite/Shared%20Documents/UserGiude-WayBill.pdf";
   WebClient client = new WebClient();
   Byte[] buffer = client.DownloadData(path);
   if (buffer != null)
   {
       HttpContext.Current.Response.ContentType = "application/pdf";
       HttpContext.Current.Response.AddHeader("content-length", buffer.Length.ToString());
       HttpContext.Current.Response.BinaryWrite(buffer);
   }     
}

This is a error:

enter image description here

With hyperLink This code download the file(with IE)... I want Open a file in new tab... this is possible with IE Edge.., How to do this with IE ?

HyperLink href = new HyperLink();
    href.NavigateUrl = "http://sf-spsdev02/TestSite/Shared%20Documents/UserGiude-WayBill%20System.pdf";
    href.Text = "Помощ...";
    href.Target = "_blank";
Was it helpful?

Solution

Why you are using SPLinkButton?

You can simply use HyperLink with its attribute called Navigateurl from your code.

You can follow below steps in below mentioned URL.

Open PDF files from a Document Library in a New Window

Follow this

  1. Open IIS manager (Start | Control Panel | Administrative Tools | Internet Information Services Manager).
  2. Expand the “Application Pools” node.
  3. Right click the application pool which your project is using, and then select “Properties”.
  4. Click “Identity” tab.
  5. Choose “Local System” in the Predefined dropdown list.

About how to grant permission to a file, please check these steps:

  1. Open Windows Explorer.
  2. Right click the file, and then select "Properties".
  3. Click the "Security" tab.
  4. Add "Network Service" in access list and check "Modify", "Read", and "Write" for it.

and finally

in your code add this

System.Net.WebRequest request = System.Net.HttpWebRequest.Create("http://localhost/Webservicetest1/Service.asmx/webmrthod");

request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;

System.Net.WebResponse response = request.GetResponse();

Remember that below lines are important for you

request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;

It is working without any problem..

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top