0

我正在处理从服务器的 Z:/File 目录显示文件。问题是,虽然 PDF 和 JPEG/JPG 文件在 localhost 的 Iframe 中正确呈现,但当我使用 IIS 服务器 IP 名称 192.168.xxx.xxx:8081/Home.aspx 时,它们不会呈现。我还有一个下载按钮,用户可以在其中下载文件。Iframe 和下载按钮指向相同的源,但 Iframe 无法正确返回/显示文件。它只是显示空白。

以下是源 URL 的示例:\192.168.xxx.xxx\Z$\File Directory\PDF Files\cyber.pdf。

哦,顺便说一句,我还将它们动态映射到 iframe 和下载按钮。

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");
        }
    }

这是第一页中的代码,因为用户单击文件以查看/下载它。下一页是..

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");   
        }
    }

下载按钮是:

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");
        }
    }

它们在 localhost 中运行良好,但在 IIS 服务器中不显示。有小费吗?

4

1 回答 1

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.

于 2013-07-23T05:32:03.723 回答