Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi everyone..
i successfully passed some data from ajax to handler...but i cant able to pass the context(image request)..
here is my code in handler


please hit the breakpoint in handler and run it..the run flow is not going to upload function


C#
public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {


        if (context.Request.QueryString["action"] == "upload")
        {


            string id = context.Request.QueryString["iden"].ToString();
            if (id != null)
            {

                upload(id, context);

            }
  }
    }
    
 private void upload(string ProductID, HttpContext context)
        {
            
            if (context.Request.Files.Count == 1)
            {

                HttpPostedFile file = context.Request.Files[0];
                file.SaveAs(context.Server.MapPath(".\\ProductImage\\ProductID\\") + file.FileName);
                context.Response.Write("File uploaded");
            }
            
            
        }

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

// here is my ajax code

C#
           $.ajax({
                url: "new_prod.aspx/insert",
                type: "post",
                data:'{}',
                dataType: "json",
                success: function (response) {
                    alert(response.d);
                    var ident = response.d;
                    identi = ident;
                    $.ajax({
                    
                        url: "Handler.ashx?action=upload&iden=" + identi,
                        type: "post",
                        allowedTypes: "png,gif,jpg,jpeg"
                    });
                },
                failure: function (response) {
                    alert("failure");
                }
            });
});


here is my c# code

C#
[System.Web.Services.WebMethod]
public static long insert(string pcategory, string category, string scategory, string ssno, string sname, string scity, string pcode, string soption, string primage, string ititle, string ilocation, string pprice, string pweight, string avail, string pdes, string swoption)
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]);

    SqlCommand cmd = new SqlCommand();
    DataSet ds = new DataSet();
    SqlDataAdapter adapt = new SqlDataAdapter();
    con.Open();
    cmd.CommandText = "insert into new_prod(parent_category,category,sub_category,supplier_style_no,supplier_name,supplier_city,product_code,size_option,prod_image,image_title,image_location,prod_price,prod_weight,availability,prod_description,switc_option) values ('" + pcategory + "','" + category + "','" + scategory + "','" + ssno + "','" + sname + "','" + scity + "','" + pcode + "','" + soption + "','" + primage + "','" + ititle + "','" + ilocation + "','" + pprice + "','" + pweight + "','" + avail + "','" + pdes + "','" + swoption + "')";
    string join = cmd.CommandText + "select @@identity";
    cmd = new SqlCommand(join, con);
    long ReturnType_long = Convert.ToInt64(cmd.ExecuteScalar());


    //cmd.Connection = con;

    //cmd.ExecuteNonQuery();
    con.Close();
    return ReturnType_long;


}
Posted
Updated 10-Jul-14 21:53pm
v5

1 solution

The context argument is basically the webpage.
I'm not sure if this is the best way to do up loads. The FileUpLoad control handles this for you. If you're doing downloads, that would be a bit different as I use a handler for those.

Do you have the tags in the web.config?

HTML
<httpHandlers>
  <add verb="*" path="Download.aspx" type="Namespace.DownloadHandler,AssemblyName" validate="false" />
</httpHandlers>

and/or (depending on what runtime version you're using. I just keep both in it)
HTML
<handlers>
    <add name="Download" verb="*" path="Download.aspx" type="Namespace.DownloadHandler,AssemblyName" resourceType="Unspecified" />

</handlers>
 
Share this answer
 
v2
Comments
Member 10937448 25-Jul-14 6:21am    
Thanks for your reply -Dr_X-...it helped me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900