Vishal Patel
Vishal Patel

Reputation: 973

I can't open .pdf, .doc etc. file from other servers via SFTP using rebex

I have Two different servers as like below.

1. WebServer - Where is my application is locate

2. FileServer - where is my all uploaded files are locate in perticular location / Folder

Now in my web application I have created one page which display all uploaded files from FileServer (which i have uploaded).

But the issue is, when I try to open/read that file then I am not able. I have all rights of SFTP. and I am getting below issue in Try-catch block. "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack." I have used REBEX for upload and download files.

Note : I can able to open simple text file (.txt) but not other formated files like (.PDF, .docx, .jpg etc.)

My file read code is like below.

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)
                {

                }
}

So any one can help to fixe this issue.

I am using latest Mozila browser.

Thank you.

Upvotes: 0

Views: 421

Answers (1)

Tomáš Knopp
Tomáš Knopp

Reputation: 26

Would it be possible to post the complete stack trace of the exception here? You can get the stack trace by modifying the catch block like this:

            try
            {
                //... your code
            }
            catch (Exception ex)
            {
                Response.ContentType = "text/plain";
                Response.Write(ex.ToString());
                Response.End();
            }

Upvotes: 0

Related Questions