C# 查询字符串到处理程序页

C# 查询字符串到处理程序页,c#,asp.net,http,c#-4.0,C#,Asp.net,Http,C# 4.0,如何从database.aspx页面上的photopath到handler.ashx页面创建查询字符串 我希望处理程序页面能够在我的photopath字符串中找到以下内容: protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { protected void GridView1_SelectedIndexChanged(object sender, EventArgs

如何从database.aspx页面上的photopath到handler.ashx页面创建查询字符串

我希望处理程序页面能够在我的photopath字符串中找到以下内容:

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string PhotoPath;

        GridViewRow row = GridView1.Rows[GridView1.SelectedIndex];

        PhotoPath = row.Cells[5].Text;
        PhotoPath = HttpUtility.UrlEncode(PhotoPath);

        HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create(PhotoPath);
        HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();
        Stream resStream = response.GetResponseStream();
        using (System.Drawing.Image img = System.Drawing.Image.FromStream(resStream))
        {
            img.Save("temp.jpg", ImageFormat.Jpeg);
        }

    }
}
然后在我的GetImage.ashx处理程序页面中检索它:

    public class GetImage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            {
            string PhotoPath = System.Web.HttpContext.Current.Request.QueryString["PhotoPath"];
            PhotoPath = HttpUtility.UrlDecode(PhotoPath);
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(PhotoPath));
            request.Method = WebRequestMethods.Ftp.DownloadFile;    
            request.Credentials = new NetworkCredential("Administrator", "commando");

            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.Clear();
                context.Response.ClearHeaders();
                context.Response.ClearContent();
                context.Response.ContentType = "image/jpeg";
                context.Response.BinaryWrite(mStream.GetBuffer());

            }
            catch (WebException wex)
            {

                //throw new Exception("Unable to locate or access your file.\\nPlease try a different file.");

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

            }

        }
    }

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

在web应用程序端的事件处理程序中,只需将url设置为如下内容

http://myserver/GetImage.ashx?PhotoPath=\\photoserver\item.gif
在Http处理程序代码中,您只需从ProcessRequest方法的HttpContext参数中读取它,如下所示

string path = context.Request.QueryString["PhotoPath"];

在web应用程序端的事件处理程序中,只需将url设置为如下内容

http://myserver/GetImage.ashx?PhotoPath=\\photoserver\item.gif
在Http处理程序代码中,您只需从ProcessRequest方法的HttpContext参数中读取它,如下所示

string path = context.Request.QueryString["PhotoPath"];

我一直收到一个错误:在这一行上:FtpWebRequest=FtpWebRequestFtpWebRequest.Createnew-UriPhotoPath;值不能为null。参数名称:uriStringthat错误是由PhotoPath变量中的空值引起的。你确定它被传进来了吗?看起来您正在使用行中单元格5的文本,其格式应与我的示例中的格式相同。至于使用FTP服务器,我想您必须从处理程序中的配置中读取FTP地址和/或路径,除非我误解了您。我一直收到一个错误:在这一行上:FtpWebRequest=FtpWebRequestFtpWebRequest.Createnew UriPhotoPath;值不能为null。参数名称:uriStringthat错误是由PhotoPath变量中的空值引起的。你确定它被传进来了吗?看起来您正在使用行中单元格5的文本,其格式应与我的示例中的格式相同。至于使用FTP服务器,我想您必须从处理程序的配置中读取FTP地址和/或路径,除非我误解了您。