开发者

read stream into image control error in uri path

开发者 https://www.devze.com 2023-01-28 07:06 出处:网络
Hey trying to read stream into image control can any one help also I have some errors in my handler page.

Hey trying to read stream into image control can any one help also I have some errors in my handler page.

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();
    // want to output to image control upon selection of gridview
    //using (System.Drawing.Image img = System.Drawing.Image.FromStream(resStream))
    //{
    //    img.Save("temp.jpg", ImageFormat.Jpeg);
    //}

}

}

I get the error Value cannot be null. Parameter name: uriString

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)); 
// error is here -------------------
        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(mSt开发者_如何学JAVAream.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;
    }
}

}


As I explained earlier, you need to set the URL in the <img> tag to include the PhotoPath in the querystring.

0

精彩评论

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

关注公众号