【问题标题】:How to pass id parameter to .ashx file upload如何将 id 参数传递给 .ashx 文件上传
【发布时间】:2020-11-23 02:54:21
【问题描述】:

嗨,我有这个 Ocap ID,它是从 DB 序列自动生成的 ID,我使用 jquery ajax my reference of file upload 上传了这个文件,我想将文件路径和 Ocap ID $('#ocapID').val() 插入数据库 在 .ashx 文件中,但如何将 Ocap ID 传递给 .ashx 文件

我已经在互联网上搜索过,但我不确定要查找什么。

希望有人帮助我提前谢谢你

【问题讨论】:

    标签: c# jquery asp.net .net


    【解决方案1】:

    我的示例代码是这样的。它将返回一个具有属性 Id 和 Name 的对象。

    javascript 与 jquery

    <script>
        $.ajax({
            url: 'Handler1.ashx',
            data: { itemId : 100 },
            type: 'GET',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {
                if (data) {
                    alert(data.Name);
                }
            }
        });
    </script>
    

    处理程序 ashx

    public class Handler1 : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
            string szItemId = context.Request.QueryString["itemId"] ?? string.Empty;
            if (string.IsNullOrEmpty(szItemId)) return;
    
            int nItemId = -999;
            if (!int.TryParse(szItemId, out nItemId) || nItemId <= 0) return;
    
            Student stu = new Student(nItemId);
    
            context.Response.ContentType = "application/json";
            context.Response.Charset = "utf-8";
            context.Response.Write(new JavaScriptSerializer().Serialize(stu));
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    
    public class Student
    {
        public Student(int id)
        {
            Id = id;
            Name = "Test";
        }
    
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多