gpt4 book ai didi

c# - jQuery Ajax 文件上传到带有 JSON 响应的 ASP.NET Web 服务

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:00 27 4
gpt4 key购买 nike

我正在尝试使用 jQuery Ajax 将文件上传到 c# Web 服务 (.asmx)。然后该文件由 Web 服务处理,操作结果异步返回给调用 JavaScript。

文件上传有效。但是,它要求我省略选项 contentType: 'application/json; charset=utf-8' 调用 $.ajax() 函数时。这会导致结果不会序列化为 XML 而不是预期的 JSON。而这又会导致 jQuery 调用 error 处理程序而不是 success 处理程序。

这是我的客户端代码:

$.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
  • 或者找到一种方法强制 WebService 始终将返回值序列化为 JSON,无论客户端说什么。

有什么想法吗?

提前致谢并致以亲切的问候,

克里斯。

最佳答案

你可以使用 Ajax 库 query.ajax_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>

在服务器端,你可以编写httpFile handler;

public class FileHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//For multiple files
foreach (string f in context.Request.Files.AllKeys)
{
HttpPostedFile file = context.Request.Files[f];
if (!String.IsNullOrEmpty(file.FileName)) {
string test = file.FileName;
}
}
}
}

关于c# - jQuery Ajax 文件上传到带有 JSON 响应的 ASP.NET Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24183401/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com