FTP 处理程序页面,从 aspx 帮助中调用字符串以及初始化处理程序页面的方法

发布于 2024-10-06 07:06:48 字数 3056 浏览 1 评论 0 原文

嘿伙计们,试图清理我的上一篇文章,所以就到这里了。

我的 asp.net 项目中有一个处理程序页面,它“尝试”处理 ftp 下载请求。

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

}

在我的包含 gridview 的页面上,我有一种方法可以从我的一个单元格中提取数据,该单元格包含我试图在图像控件中显示的图像的 ftp 路径和文件名:

Database.aspx。 程序

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

我已将图像控件 ImageUrl 设置为 ~/GetImage.ashx ,

在我的处理程序页面中,我想调用字符串“PhotoPath”并将其放入处理程序页面中的 ftwwebrequest 中:

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

并且需要一些代码帮助来调用处理 我的数据库页面?

任何人都可以帮忙吗,因为我已经坚持这个问题很多年了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2024-10-13 07:06:48
  • 您需要使用查询字符串将 PhotoPath 传递给 ASHX 处理程序。然后,您应该从查询字符串中读取参数并将其传递给 WebRequest.Create
  • 不应该吞下异常
  • 您不需要清除响应
  • 您应该将 FtpWebResponse 直接复制到 HttpResponse;您不需要中间 MemoryStream
  • ContentType 属性告诉浏览器您要发送的文件类型。请参阅http://en.wikipedia.org/wiki/MIME_type
  • 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文