我正在使用c#中的jquery和http处理程序来上载图像...我想在处理程序中使用文件上载数据发送多个可变值..我的代码如下..这是我的jquery代码...
var filename = document.getElementById("<%=fileuploadimages.ClientID %>");
var files = fileUpload.files;
var test = new FormData();
for (var i = 0; i < files.length; i++) {
test.append(files[i].name, files[i]);
}
$.ajax({
url: "UploadHandler.ashx",
type: "POST",
contentType: false,
processData: false,
data: test,
// dataType: "json",
success: function (result) {
alert(result);
},
error: function (err) {
alert(err.statusText);
}
});
这是我用于上传图片的处理程序代码
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 fname;
if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = testfiles[testfiles.Length - 1];
}
else
{
fname = file.FileName;
}
fname = Path.Combine(context.Server.MapPath("~/UserImage/"), fname);
file.SaveAs(fname);
}
}
context.Response.ContentType = "text/plain";
context.Response.Write("File Uploaded Successfully!");
}
public bool IsReusable
{
get
{
return false;
}
}
一切正常,我的问题是我想用test发送另一个变量。如var a =“ xxx” ..我也要用test发送变量。如何做到这一点?可以吗?一个可以帮我吗?
您应该使用方法“ append”将更多项添加到FormData对象,如下所示:
Javascript:
var test = new FormData(); test.append("txtHome",$("#txtHome").val()); //Name and value
在您的处理程序中:
context.Request.Form["txtHome"]; //Get the value with the name of variable
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句