如何将更多参数传递给ashx asp.net?

时间:2016-01-18 08:27:11

标签: c# asp.net ajax ashx form-data

关于这个,我创造了更多的时间,但似乎它没有用。我有2个文本框和1个文件输入,我想上传一些信息和文件(图像)。这是我的代码片段。首先是文件.ashx

public void ProcessRequest(HttpContext context)
{
string text1 = context.Request["text1_temp"];
string number = context.Request["number_temp"];
HttpFileCollection files = context.Request.Files;
/////something 
}

和.aspx文件

var files = $("#inputfile").get(0).files;
var test = new FormData();
///// only 1 file image
for (var i = 0; i < files.length; i++) {
test.append(files[i].name, files[i]);
}
///// add more parameter
test.append($("#txt_text1").val(), "text1_temp");
test.append($("#txt_number").val(), "number_temp");

if(!isImage($("#inputfile").val())){
alert("Not file Image");
return false;
}
else{
$.ajax({
url: "UploadPaper.ashx",
type: "POST",
contentType: false,
processData: false,
data: test,
success: function (result) {
console.log(result);
},
error: function (err) {
console.log(err.statusText);
}
});
return true;
}

好的,看起来似乎很有效,但是当我在string text1 = context.Request["text1_temp"];string number = context.Request["number_temp"];运行并调试时,数据text1和number为null。 你能告诉我这里有什么问题或错误吗?以及如何解决它? 非常感谢你。

1 个答案:

答案 0 :(得分:-1)

因为您要发送一个对象,而不是不同的参数。

相反,您可以使用:

data: "{text1_temp: 'myValue',number_temp: '0'}",
相关问题