簡體   English   中英

查詢字符串到處理程序頁面

[英]query string to handler page

如何在我的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"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM