文件上载在通用处理程序中获取额外参数

时间:2017-09-21 00:29:26

标签: c# jquery file-upload generic-handler

我一直在拔头发。我需要将参数传递给ashx以进行文件上传。我正在使用blueimp。我在很多不同的帖子上都看到我使用" formData"参数,实现这一点,但我不知道如何在通用处理程序中访问信息。这是我用来发布给处理程序的代码:

theDatabase ^.. each . _DbDate

一旦我进入C#处理程序,我如何获得" filename"?

        $(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');
            }
        });
    });

谢谢。

1 个答案:

答案 0 :(得分:0)

之前我做过,我喜欢这样:

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();

                }


            }
        }
}

然后在您的HTML视图中:

<img ID="_photoImage" name="_photoImage" runat="server"  Width="100" Height="150" src="~/PhotoHandler.ashx?search=@ViewBag.empid" />