如何使用Jquery / Ajax调用将webform的文本框和文件上传数据发送到webmethod? [英] How can I send textbox and file upload data of a webform to webmethod using Jquery/Ajax call?
本文介绍了如何使用Jquery / Ajax调用将webform的文本框和文件上传数据发送到webmethod?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含一些文本框和文件上传的表单。我正在尝试使用.ajax()方法将此数据发送到webmethod。到目前为止我甚至都没能打到断点。
var dataToSend = new FormData( );
dataToSend.append(' file', document .getElementById( myFile)。value);
dataToSend.append(' text', document .getElementById( biddername)。value);
$ .ajax({
type: POST ,
url: SupplierMaster.aspx / RegisterSupplier,
data:dataToSend ,
processData: false ,
contentType: false ,
dataType : false ,
async: true ,
成功: function (data,status){
console .log( CallWM);
alert(data.d);
},
失败: function (data){
警报(data.d);
},
错误: function (data){
alert(data.d);
}
});
}
[WebMethod ]
public static string RegisterSupplier(HttpPostedFile文件,字符串 biddername)
{
return a;
}
解决方案
.ajax({
type: POST,
url: SupplierMaster.aspx / RegisterSupplier,
data:dataToSend,
processData: false ,
contentType:< span class =code-keyword> false ,
dataType: false ,
async: true ,
成功:功能(数据,状态){
console .log( CallWM);
alert(data.d );
},
失败: function (data){
alert(data.d);
},
错误: function (data){
alert(data.d);
}
});
}
[WebMethod ]
public static string RegisterSupplier(HttpPostedFile文件,字符串 biddername)
{
return a;
}
由于javascript无法访问文件系统以序列化数据,因此无法上传文件。谷歌html5 asynch文件上传为那些使用html5的客户提供解决方案,对于非html5浏览器,你需要使用异步上传插件。谷歌asp.net asynch上传,你会发现很多实例
I have a form that has some textboxes and a file upload. I am trying to send this data to webmethod using .ajax() method. I have not even been able to hit the breakpoint so far.
var dataToSend = new FormData();
dataToSend.append('file', document.getElementById("myFile").value);
dataToSend.append('text', document.getElementById("biddername").value);
$.ajax({
type: "POST",
url: "SupplierMaster.aspx/RegisterSupplier",
data: dataToSend,
processData: false,
contentType: false,
dataType: false,
async: true,
success: function (data, status) {
console.log("CallWM");
alert(data.d);
},
failure: function (data) {
alert(data.d);
},
error: function (data) {
alert(data.d);
}
});
}
[WebMethod]
public static string RegisterSupplier(HttpPostedFile file, string biddername)
{
return "a";
}
解决方案
.ajax({ type: "POST", url: "SupplierMaster.aspx/RegisterSupplier", data: dataToSend, processData: false, contentType: false, dataType: false, async: true, success: function (data, status) { console.log("CallWM"); alert(data.d); }, failure: function (data) { alert(data.d); }, error: function (data) { alert(data.d); } }); }
[WebMethod] public static string RegisterSupplier(HttpPostedFile file, string biddername) { return "a"; }
You can't upload files that way as javascript can't access the filesystem to serialise the data. Google "html5 asynch file upload" for a solution for those clients that use html5, and for non-html5 browsers you'll need to use an asynch upload plug in. Google "asp.net asynch upload" and you'll find lots of examples.
这篇关于如何使用Jquery / Ajax调用将webform的文本框和文件上传数据发送到webmethod?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文