2010-12-05 14 views
1

database.aspxページのphotopathからmy 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ハンドラページでそれを取得するには:Web上

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

答えて

4

アプリケーション側(あなたのイベントハンドラ)、ちょうどこのようなものにあなたのURLを設定します。

http://myserver/GetImage.ashx?PhotoPath=\\photoserver\item.gif 

とHTTPハンドラのコードで、あなただけそうのようなProcessRequest方法のHttpContextパラメータからそれを読んで。

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

私はエラーを取得しておいてください。この行に:たFtpWebRequest要求=(たFtpWebRequest)FtpWebRequest.Create(新しいウリ(PhotoPath));値はnullにはできません。 パラメータ名:uriString –

+0

このエラーは、PhotoPath変数のnull値によって発生します。あなたはそれが渡されていると確信していますか?あなたの行にあるセル5のテキストを使用しているようですが、これは私の例のフォーマットでなければなりません。 FTPサーバーを使用する場合は、誤解していない限り、ハンドラーの設定からftpアドレスおよび/またはパスを読み取らなければならないと思います。 –