0

我有两个不同的服务器,如下所示。

1. WebServer - 我的应用程序在哪里

2. FileServer - 我所有上传的文件都位于特定位置/文件夹中

现在在我的 Web 应用程序中,我创建了一个页面,显示从 FileServer 上传的所有文件(我已上传)。

但问题是,当我尝试打开/读取该文件时,我无法做到。我拥有 SFTP 的所有权利。我在 Try-catch 块中遇到了以下问题。 “无法评估表达式,因为代码已优化或本机框架位于调用堆栈顶部。” 我使用 REBEX 上传和下载文件。

注意:我可以打开简单的文本文件(.txt),但不能打开其他格式的文件,如(.PDF、.docx、.jpg 等)

我的文件读取代码如下所示。

protected void LinkButton1_Click(object sender, EventArgs e)
        {    
          try
                {
                    LinkButton lbtn = (LinkButton)sender;
                    using (Sftp _sftp = new Sftp())
                    {
                        _sftp.Connect(serverNm, 22);
                        _sftp.Login(username, password);
                        _sftp.ChangeDirectory(currentDirectory);

                        Stream stream = _sftp.GetStream(lbtn.ToolTip.ToString(), FileMode.Open, FileAccess.Read);
                        StreamReader sourceStream = new StreamReader(stream);
                        Byte[] buffer = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

                        Response.Buffer = true;
                        if (lbtn.ToolTip.IndexOf(".pdf") > 0)
                        {
                            Response.ContentType = "application/pdf";
                        }
                        else if (lbtn.ToolTip.IndexOf(".txt") > 0)
                        {
                            Response.ContentType = "text/plain";
                        }
                        else if (lbtn.ToolTip.IndexOf(".xls") > 0)
                        {
                            Response.ContentType = "application/ms-excel";
                        }
                        else if (lbtn.ToolTip.IndexOf(".doc") > 0)
                        {
                            Response.ContentType = "application/msword";
                        }
                        else if (lbtn.ToolTip.IndexOf(".jpeg") > 0)// || lbtn.ToolTip.IndexOf(".jpg") > 0)
                        {
                            Response.ContentType = "image/jpeg";
                        }
                        else if (lbtn.ToolTip.IndexOf(".jpg") > 0)
                        {
                            Response.ContentType = "image/jpg";
                        }
                        else if (lbtn.ToolTip.IndexOf(".bmp") > 0)
                        {
                            Response.ContentType = "image/png";
                        }

                        Response.AddHeader("Content-Length", "attachment;filename=" + buffer.Length.ToString());
                        Response.BinaryWrite(buffer);
                        Response.End();                    

                    }
                }
                catch (Exception ex)
                {

                }
}

所以任何人都可以帮助解决这个问题。

我正在使用最新的 Mozilla 浏览器。

谢谢你。

4

1 回答 1

0

是否可以在此处发布异常的完整堆栈跟踪?您可以通过修改 catch 块来获取堆栈跟踪,如下所示:

            try
            {
                //... your code
            }
            catch (Exception ex)
            {
                Response.ContentType = "text/plain";
                Response.Write(ex.ToString());
                Response.End();
            }
于 2014-10-07T15:27:39.580 回答