簡體   English   中英

我正在嘗試上傳圖像並綁定Gridview以顯示圖像?

[英]I'm trying to upload image and binding Gridview to display image?

我有一個上傳圖像並將Gridview綁定到顯示圖像的任務。

我正在使用通用處理程序,並引發以下錯誤:“類型HttpHandler不繼承'System.web.IHttpHandler'形式。”

有人可以幫忙嗎?

我的代碼:HttpHandler.ashx

<%@ WebHandler Language="C#" Class="HTTPHandler" %>

using System;
using System.Web;
using System.Web.Configuration;
using System.Configuration;
using System.Data.SqlClient;

public class HTTPHandler  { //IHttpHandler {

    string strcon = ConfigurationManager.AppSettings["ImagConnectionString2"].ToString();

    public void ProcessRequest (HttpContext context) {
        string imageid = context.Request.QueryString["ImageId"];
        SqlConnection connection = new SqlConnection(strcon);
        connection.Open();
        SqlCommand command = new SqlCommand("select Image from image where ImageId=" + imageid, connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((Byte[])dr[0]);
        connection.Close();
        context.Response.End();
    }
}

我的代碼:Button_Click

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Configuration;
using System.Data;
using System.Data.SqlTypes;

public partial class _Default : Page
{
    string strcon = ConfigurationManager.ConnectionStrings["ImagConnectionString2"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridData();
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fileuploadImage.HasFile)
        {
            int length = fileuploadImage.PostedFile.ContentLength;

            //create a byte array to store the binary image data
            byte[] imgbyte = new byte[length];

            //store the currently selected file in memeory
            HttpPostedFile img = fileuploadImage.PostedFile;

            //set the binary data
            img.InputStream.Read(imgbyte, 0, length);
            string imagename = txtImageName.Text;

            //use the web.config to store the connection string
            SqlConnection connection = new SqlConnection(strcon);
            connection.Open();

            SqlCommand cmd = new SqlCommand("INSERT INTO image (ImageName,Image) VALUES (@imagename,@imagedata)", connection);
            cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename;
            cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
            int count = cmd.ExecuteNonQuery();

            connection.Close();
            if (count == 1)
            {
                BindGridData();
                txtImageName.Text = string.Empty;

                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true);
            }
        }

    }

    private void BindGridData()
    {
        SqlConnection connection = new SqlConnection(strcon);
        SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [image]", connection);
        SqlDataAdapter daimages = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        daimages.Fill(dt);
        gvImages.DataSource = dt;
        gvImages.DataBind();
        gvImages.Attributes.Add("bordercolor", "black");

    }
}

創建客戶處理程序時,仍然需要繼承接口。 請嘗試從“ IHttpHandler”類繼承:

public class HTTPHandler : IHttpHandler

暫無
暫無

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

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