【问题标题】:File Upload Get Extra Parameters In Generic Handler文件上传在通用处理程序中获取额外参数
【发布时间】:2018-03-02 04:43:49
【问题描述】:

我一直在努力解决这个问题。我需要将参数传递给 ashx 以进行文件上传。我正在为此使用blueimp。我在许多不同的帖子中看到我将使用“formData”参数来完成此操作,但我不知道在通用处理程序中如何访问信息。这是我用来发布到处理程序的代码:

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

进入 C# 处理程序后,如何获取“文件名”?

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

谢谢。

【问题讨论】:

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


    【解决方案1】:

    我以前做过,现在是这样的:

    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" />
    

    【讨论】:

    • 这只是如何使用 .ashx 文件的指南
    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多