Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

c# - FTP handler page, call string from aspx help with method to initialize handler page

Hey guys, tryed to clean up my last post so here it goes.

I have a handler page in my asp.net project that is "trying" to handle a ftp download request.

GetImage.ashx.cs

public class GetImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        {

            // method for calling PhotoPath from Database.aspx.cs
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri());
            // (new Uri) I would like to just store "photopath" in here as it has the ftp plus the filename in it
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            request.Credentials = new NetworkCredential("username", "password");

            try
            {
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                Stream stream = response.GetResponseStream();
                byte[] bytes = new byte[2048];

                int i = 0;
                MemoryStream mStream = new MemoryStream();

                do
                {

                    i = stream.Read(bytes, 0, bytes.Length);

                    mStream.Write(bytes, 0, i);
                } while (i != 0);

                context.Response.ClearHeaders();
                context.Response.ClearContent();
                context.Response.ContentType = "image/jpeg";
// Response.Headers.Add("Content-Type", "image/jpeg"); this is from the database.aspx page not sure if its correct 
                    context.Response.BinaryWrite(mStream.GetBuffer());
                }
                catch (WebException wex)
                {

            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred: " + ex);

            }

        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

}

On my page that holds my gridview I have a method to extract data from one of my cells, this cell contains the ftp path and filename of the image im trying to show in my image control:

Database.aspx.cs

protected void Page_Load(object sender, EventArgs e){

        Response.Headers.Add("Content-Type", "image/jpeg");

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string PhotoPath = "";
        // Get the currently selected row. Because the SelectedIndexChanging event
        // occurs before the select operation in the GridView control, the
        // SelectedRow property cannot be used. Instead, use the Rows collection
        // and the NewSelectedIndex property of the e argument passed to this 
        // event handler.
        GridViewRow row = GridView1.Rows[GridView1.SelectedIndex];

        PhotoPath = row.Cells[5].Text;
        // how do I send photopath to my handler page?
        //TextBox1.Text = PhotoPath;
    }
}

}

In my handler page I want to call the string "PhotoPath" and place it in my ftwwebrequest in my handler page:

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(PhotoPath));

Ive set my image control ImageUrl to ~/GetImage.ashx and will need some help with code to call the handler from my database page?

Can any one help because ive been stuck on this for ages?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • You need to pass PhotoPath to the ASHX handler using the querystring. You should then read the parameter from the querystring and pass it to WebRequest.Create
  • You should not swallow exceptions
  • You don't need to clear the response
  • You should copy the FtpWebResponse directly to the HttpResponse; you don't need an intermediate MemoryStream
  • The ContentType property tells the browser what kind of file you're sending. See http://en.wikipedia.org/wiki/MIME_type

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...