开发者

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

开发者 https://www.devze.com 2023-01-29 03:21 出处:网络
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.

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.Ft开发者_运维技巧p.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?


  • 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
0

精彩评论

暂无评论...
验证码 换一张
取 消