cordova 项目 在html页面使用jQuery ajax上传文件
使用formData传输图片
后台使用ashx接收文件 显示接收文件数量为0
哪位大神帮忙指导一下 为什么后台接收不到文件
代码呢?用jquery+FormData的话需要设置processData为false不处理数据为键值对
processData 和 contentType 都设置为false了
```//页面ajax发送
var formData = new FormData($("#btnImg")[0].files);
$.ajax({
type: "POST",
url: strUrl,
data: formData,
processData: false,
contentType: false,
success: function (data) {
},error: function (err) {
}
})
后台ashx接收
public void insertImg (HttpContext context) {
context.Response.ContentType = "text/plain";
string result = string.Empty;
string filePath = string.Empty;
string fileNewName = string.Empty;
try {
HttpFileCollection files = context.Request.Files;
if (files.Count > 0)
{
fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + System.IO.Path.GetFileName(files[0].FileName);
try { files[0].SaveAs(context.Server.MapPath("~/saveImg/"+fileNewName)); }
catch {
context.Response.Write("Error:"+filePath.ToString());
context.Response.End();
return;
}
result = "OK," + fileNewName + "";
}
else
{
result = "Error:错误代码:02,上传失败!";
}
}
catch
{
result = "Error:错误代码:03,上传失败!";
}
context.Response.Write(result);
context.Response.End();
}