0

I've been pulling my hair out on this. I need to pass a parameter to the ashx for my file upload. I'm using blueimp for this. I've seen on many different posts that I'm to use the "formData" parameter, to accomplish this, but I have no idea how to access the info once I'm in the Generic Handler. This is the code I use to post to the handler:

        $(document).ready(function () {
        $('#btnFileUpload').fileupload({
            url: 'FileUploader.ashx?upload=start',


           //This is what I want
           //-----------------------
           formData: {filename: document.getElementById("txtContractUploadName").value},
           //-----------------------


            add: function (e, data) {
                    $('#progressbar').show();
                    data.submit();
            },
            progress: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#progressbar div').css('width', progress + '%');
            },
            success: function (response, status) {
                $('#progressbar').hide();
                $('#progressbar div').css('width', '0%');
                console.log('success', response);
                switch (response.toLowerCase()) {
                    case "success":
                        ShowNotificationBar('Success!', 1000, 'notificationSuccess');
                        break;

                    case "file size":
                        ShowNotificationBar('You may only upload files that are 5MB or less.', 2500, 'notificationFail');
                        break;

                    case "file type":
                        ShowNotificationBar('You may only upload PDF files.', 2000, 'notificationFail');
                        break;
                }

            },
            error: function (error) {
                $('#progressbar').hide();
                $('#progressbar div').css('width', '0%');
                ShowNotificationBar('There was an error with your request.', 2000, 'notificationFail');
            }
        });
    });

Once I'm in the C# handler, how do I get "filename"?

    public void ProcessRequest(HttpContext context)
    {
        //What do I do here?
    }

Thank you.

Vandel212
  • 1,074
  • 1
  • 13
  • 28

1 Answers1

0

I did it before and i do like this:

public void ProcessRequest(HttpContext context)
{
  if (context.Request.QueryString["search"] == null) return;

  string parameterContent= context.Request.QueryString["search"];


  using (MySqlConnection conn = new MySqlConnection(connstr))
        {
            using (MySqlCommand cmd = new MySqlCommand("Select imageColumn from imagetable where imageidintable = @search", conn))
            {
                cmd.Parameters.Add(new MySqlParameter("@search", parameterContent));
                conn.Open();
                using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {

                    reader.Read();
                    context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("imageColumn ")]);
                    reader.Close();

                }


            }
        }
}

Then in Your HTML View:

<img ID="_photoImage" name="_photoImage" runat="server"  Width="100" Height="150" src="~/PhotoHandler.ashx?search=@ViewBag.empid" />
Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30