0

I'm working with displaying files from the server's Z:/File Directory. The problem is, while the PDF and JPEG/JPG files render correctly inside the Iframe in localhost, when I use the IIS server IP name, 192.168.xxx.xxx:8081/Home.aspx, they do not render. I also have a download button in which the user can.. well download files. The Iframe and the download button points to the same source, but the Iframe does not return/display the file correctly. It just shows blank.

Here is an example of the source URL: \192.168.xxx.xxx\Z$\File Directory\PDF Files\cyber.pdf.

Oh and BTW, I also map them to the Iframe and download button dynamically.

protected string GetPath(TreeNode treenode)
    {
        string[] array = new string[100];
        string path = string.Empty;
        int depth = treenode.Depth;
        TreeNode node = new TreeNode();
        node = treenode;
        array[0] = node.Value;

        for (int i = 1; i <= depth; i++)
        {
            array[i] = node.Parent.Value;
            node = node.Parent; ;
        }

        //path = "~/";
        path = @"\\192.168.3.12\Z$\";

        for (int i = depth; i >= 0; i--)
        {
            if (Path.GetExtension(array[i].ToString()) == string.Empty)
            {
                //path += array[i].ToString() + "/";
                path += array[i].ToString() + @"\";
            }
            else
                path += array[i].ToString();
        }

        return path;
    }

protected void trvNews_SelectedNodeChanged(object sender, EventArgs e)
    {
        try
        {
            if (trvNews.SelectedNode.Expanded == true)
            {
                trvNews.SelectedNode.Collapse();
                trvNews.SelectedNode.Selected = false;
            }
            else if(trvNews.SelectedNode.Expanded == false)
                trvNews.SelectedNode.Expand();

            if (trvNews.SelectedNode.ChildNodes.Count == 0)
            {
                if (Path.GetExtension(trvNews.SelectedNode.Text) == string.Empty)
                {
                    hfPath.Value = GetPath(trvNews.SelectedNode);
                    //ListDirectory(trvNews, Server.MapPath(hfPath.Value), "NoChild");
                    ListDirectory(trvNews, hfPath.Value, "NoChild");
                    Session["Count"] = "Enabled";
                }
                else
                {
                    string test2 = Path.GetFullPath(hfPath.Value);
                    string path = hfPath.Value + trvNews.SelectedNode.Text;
                    //site = "DocumentViewer.aspx?=" + Path.GetFileName(path);
                    string url = "DocumentViewer.aspx?=" + Path.GetFileName(path);
                    Session["Path"] = path;
                    //ClientScript.RegisterStartupScript(typeof(Page), "Sigma", "open_win()", true);
                    ScriptManager.RegisterClientScriptBlock(this, GetType(), "newpage", "open_win('" + url + "');", true);
                    Session["Count"] = "Enabled";
                }
            }
            string test = Session["Count"].ToString();

            if (Session["Count"].ToString() == "Enabled")
                btnBack.Visible = true;
        }
        catch (Exception ex)
        {
            LogError(ex, "User");
        }
    }

This is the code in the first page as the user clicks the file to view/download it. The next page is..

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                string path = Session["Path"].ToString();
                int length = path.Length;
                lblHead.Text = Path.GetFileName(path);
                System.IO.FileInfo file = new System.IO.FileInfo(Session["Path"].ToString());

                if (Path.GetExtension(path) == ".pdf")
                {
                    pnlPdf.Visible = true;

                    if (Session["FromNews"] != null)
                        framePdf.Attributes["src"] = FormulatePathPDFNews(path);
                    else
                    {
                        framePdf.Attributes["src"] = "\\\\" + file.FullName;
                    }
                }
                else if (Path.GetExtension(path) == ".jpeg" || Path.GetExtension(path) == ".jpg")
                {
                    pnlJpeg.Visible = true;
                    //imageJpeg.Attributes["src"] = FormulatePath(path);
                    imageJpeg.Attributes["src"] = file.FullName;
                }
            }
        }
        catch (Exception ex)
        {
            LogError(ex, "User");   
        }
    }

The download button for this is:

protected void btnDownload_Click(object sender, EventArgs e)
    {
        try
        {
            if (Path.GetExtension(Session["Path"].ToString()) != null)
            {
                System.IO.FileInfo file = new System.IO.FileInfo(Session["Path"].ToString());
                if (file.Exists)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.WriteFile(file.FullName);
                    Response.End();
                }
                else
                    Response.Write("This file does not exist.");
            }
        }
        catch (Exception ex)
        {
            LogError(ex, "User");
        }
    }

They are working perfectly fine in localhost, but does not display when in IIS server. Any tips?

  • Does the account running the site under IIS have permissions to the network directory and it's files? – Sethcran Jul 23 '13 at 04:08
  • How do I set permissions in the IIS? I set the directories to be seen by all just to test the idea. But it is still the same. The files cannot be displayed. – Jeph Elijay Jul 23 '13 at 05:47

1 Answers1

0

Most of the time this would indicate that the path cannot be found or the folder required permissions.

The site will run under a specific user (Look at the Identity in the Application Pool), that user will need permissions to the folder.

Mark Redman
  • 24,079
  • 20
  • 92
  • 147
  • I am truly sorry because I have been using IIS just now. I'm just new to these ideas. Anyway, the identity of the pool the the site has been using is LocalSystem. And yes, oftentimes it returns back an error that the path can't be found. But when I look at the statement, the path in there is not what I map to the Iframe. It returns "C:\inetpub\wwwroot\192.168.xxx.xxx\" then the url of the file, which is \\192.168.xxx.xxx\Z$\File Directory and so on. I think I may have done something wrong with mapping the url? – Jeph Elijay Jul 23 '13 at 05:50
  • You cannot map the Url to a folder path unless its within the website folder structure, you will need to serve the file via an http handler or similar. – Mark Redman Jul 23 '13 at 05:58
  • Something like this: http://stackoverflow.com/questions/887985/create-png-image-with-c-sharp-httphandler-webservice – Mark Redman Jul 23 '13 at 06:16
  • So basically the httphandler will get the image, you will just need to create the correct url to hit the handler (which also requires a config setting) – Mark Redman Jul 23 '13 at 06:17
  • Does it need to have an entry in web.config? Either way, I'm going to try this one. – Jeph Elijay Jul 23 '13 at 06:21
  • So basically, the PDF file will open in the handler, then when I will just map the handler's url to the Iframe? – Jeph Elijay Jul 23 '13 at 06:24
  • Dude thanks for your help. I wouldn't have done it without you. – Jeph Elijay Jul 23 '13 at 07:40