【发布时间】:2015-11-13 14:01:33
【问题描述】:
我有一个ASP.NET c# 页面,该页面将以 JSON 格式发布带有 AJAX 的表单信息。
该信息包括Texboxes的文本和Dropdownlists的值等。
我也需要发送文件。
我已经尝试了以下代码,它工作正常:
$(".popup-content input:text,.popup-content textarea").each(function () { // Fill object by inputs
objInputs[$(this).attr("name")] = $(this).val();
});
$.ajax({ //Post information
type: "POST",
url: "myAjax.aspx",
data: { func: "Create", information: JSON.stringify(objInputs) /* Convert object to JSON string */ },
success: function (data) { // if sending was successful try to send file
var files = $("#fileImage").get(0).files; //get files form uploadfile
if (files.length > 0) {
var data = new FormData();
data.append(files[0].filename, files[0]);
$.ajax({ //Send file
type: "POST",
url: "Handler1.ashx",
contentType: false,
processData: false,
data: data,
success: function (data) {
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " " + thrownError);
},
});
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " " + thrownError);
},
});
但现在我想知道是否有办法将我的文件与我的 JSON 一起发送?
【问题讨论】:
-
到目前为止你尝试过什么?您正在使用 ASP.NET 创建什么样的应用程序?网络表格? MVC?
-
@Guanxi 我什么都没试过。并使用 ASP.NET WebForms
-
然后用谷歌搜索如何上传文件,如果您遇到任何问题,请在此处提出相关问题。我相信你可以很容易地在谷歌上找到很多关于文件上传的东西。
-
ASP.NET Ajax 使用 XMLHttpRequest 对象,所以它是 javascript。查看此帖子:stackoverflow.com/questions/2320069/jquery-ajax-file-upload.
-
我完全不清楚您要做什么?我认为您想要的内容与您的问题所要求的内容之间存在脱节。如果 Danny 提供的链接没有帮助,那么您可以考虑在您的问题中添加更多信息。