简体   繁体   English

从 JQUERY 传递数据 | AJAX 到处理器 ASP.NET

[英]pass data from JQUERY | AJAX TO HANDLER ASP.NET

I am uploading files to my app and i'am using ajax & jqyery & handel in asp.net this my code jqyery我正在将文件上传到我的应用程序,我在 asp.net 中使用 ajax & jqyery & handel 这是我的代码 jqyery

$(document).ready(function ()
        {
            $('#Button1').click(function ()
            {
                var files = $('#FileUpload1')[0].files;
                if (files.length > 0) {
                    var id = 1;
                    var formData = new FormData();
                    for (var i = 0; i < files.length; i++) {
                        formData.append(files[i].name, files[i]);
                    }
                    $.ajax({
                        url: 'Handler1.ashx',
                        method: 'Post',
                        data: formData,
                        contentType: false,
                        processData: false,
                        success: function () {
                            alert('success');
                        },
                        error: function (err) {
                            alert(err.error)
                        }
                    });
                }
            });
        });

and this my code in handler c#这是我在处理程序 c# 中的代码

public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Files.Count > 0)
            {
                HttpFileCollection files = context.Request.Files;
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];
                    string fileName = context.Server.MapPath("~/Uploads/" + System.IO.Path.GetFileName(file.FileName));
                    file.SaveAs(fileName);
                }
            }
        }

My problem is i need to pass a other variable like id from my jquery to handler someone have any suggestion please我的问题是我需要从我的 jquery 传递一个像 id 这样的其他变量给处理程序有人有任何建议

Just do the FormData.Append in your javascript只需在 javascript 中执行 FormData.Append

formData.append('id', 'test1234');

And in your handler access it via Form并在您的处理程序中通过 Form 访问它

var otherData = context.Request.Form;

Happy coding, cheers!快乐的编码,干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM