C# 使用JSON响应将jQuery Ajax文件上载到ASP.NET web服务
我正在尝试使用jqueryajax将一个文件上传到一个cweb服务.asmx。然后,Web服务处理该文件,并将操作结果异步返回给调用JavaScript 文件上传工作正常。但是,它要求我省略contentType选项:“application/json;调用$.ajax函数时,charset=utf-8'。这会导致结果不会序列化为XML而不是预期的JSON。这反过来会导致jQuery调用错误处理程序而不是成功处理程序 这是我的客户端代码:C# 使用JSON响应将jQuery Ajax文件上载到ASP.NET web服务,c#,jquery,ajax,json,file-upload,C#,Jquery,Ajax,Json,File Upload,我正在尝试使用jqueryajax将一个文件上传到一个cweb服务.asmx。然后,Web服务处理该文件,并将操作结果异步返回给调用JavaScript 文件上传工作正常。但是,它要求我省略contentType选项:“application/json;调用$.ajax函数时,charset=utf-8'。这会导致结果不会序列化为XML而不是预期的JSON。这反过来会导致jQuery调用错误处理程序而不是成功处理程序 这是我的客户端代码: $.ajax({ url: global.aja
$.ajax({
url: global.ajaxServiceUrl + '/StartStructureSynchronisation',
type: 'POST',
dataType: 'json',
//Ajax events
success: function (msg) {
// this handler is never called
error: function () {
// this handler is called even when the call returns HTTP 200 OK
},
data: data, // this is a valid FormData() object
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
[WebMethod(EnableSession = true)]
public string StartStructureSynchronisation()
{
return this.Execute(delegate
{
if (HttpContext.Current.Request.Files.Count == 0)
{
Global.StructureSyncResult = new SyncResult() { Result = false, Log = new List<string>() { "No file uploaded." } };
}
else if (!new List<string>() { ".xls", ".xlsx" }.Contains(Path.GetExtension(HttpContext.Current.Request.Files[0].FileName).ToLower()))
{
Global.StructureSyncResult = new SyncResult() { Result = false, Log = new List<string>() { String.Format("({0}) is not a valid Excel file.", HttpContext.Current.Request.Files[0].FileName) } };
}
else
{
Global.StructureSyncResult = new Synchronization().SyncStructure(HttpContext.Current.Request.Files[0].InputStream, ref Global.DocSyncCurrent, ref Global.DocSyncMax);
}
return Global.Serializer.Serialize(Global.StructureSyncResult);
});
}
这是我的服务器端代码:
$.ajax({
url: global.ajaxServiceUrl + '/StartStructureSynchronisation',
type: 'POST',
dataType: 'json',
//Ajax events
success: function (msg) {
// this handler is never called
error: function () {
// this handler is called even when the call returns HTTP 200 OK
},
data: data, // this is a valid FormData() object
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
[WebMethod(EnableSession = true)]
public string StartStructureSynchronisation()
{
return this.Execute(delegate
{
if (HttpContext.Current.Request.Files.Count == 0)
{
Global.StructureSyncResult = new SyncResult() { Result = false, Log = new List<string>() { "No file uploaded." } };
}
else if (!new List<string>() { ".xls", ".xlsx" }.Contains(Path.GetExtension(HttpContext.Current.Request.Files[0].FileName).ToLower()))
{
Global.StructureSyncResult = new SyncResult() { Result = false, Log = new List<string>() { String.Format("({0}) is not a valid Excel file.", HttpContext.Current.Request.Files[0].FileName) } };
}
else
{
Global.StructureSyncResult = new Synchronization().SyncStructure(HttpContext.Current.Request.Files[0].InputStream, ref Global.DocSyncCurrent, ref Global.DocSyncMax);
}
return Global.Serializer.Serialize(Global.StructureSyncResult);
});
}
所以,基本上,我要找的是两件事之一:
使用contentType:'application/json;字符集=utf-8'。这样,响应将被序列化为JSON。我甚至可以将该文件作为WebMethod的参数来访问,而不用使用HttpContext.Current.Request.Files。
或者找到一种方法来强制Web服务始终将返回值序列化为JSON,而不管客户机怎么说。
有什么想法吗
事先非常感谢,并致以亲切的问候
Chris。您可以使用Ajax库query.Ajax\u upload.0.6.js,使用起来很简单
我的代码只是为了提示
Button1基本上是从文件选择器对话框中选择文件的按钮
$(document).ready(function () {
function uploadFile(parameters) {
/* example 1 */
var button = $('#button1'), interval;
$.ajax_upload(button, {
action: "FileHandler.ashx?test=" + $("#paramter").val(),
name: Selectedval,
onSubmit: function (file, ext) {
StartLoading();
},
onComplete: function (file, response) {
StopLoading();
}
});
uploadButton();//Register Event
});
});
<html>
<a id="button1" style="width: 70%" class="button orange">Select File</a>
</html>
谢谢你的回答。虽然您的解决方案可能允许返回JSON序列化结果,但它完全绕过了web服务。您能想出一个仍然涉及web服务的解决方案吗?