我将在此处将我的答案与其他有效答案一起添加。首先,尽管您希望在成功函数而不是完整函数中获得返回的响应:
$("#files").kendoUpload({
async: {
saveUrl: url,
removeUrl: removeUrl,
autoUpload: true
},
select: onFileSelect, // function for when a file is selected
success: onFileSuccess, // function that returns response after upload
complete: onFileComplete, // function after success
remove: onFileRemove, // function for when a file is removed
});
on success 函数返回一个对象(通常人们将其命名为 e)
function onFileSuccess(e) {
console.log("e.response", e.response);
console.log("e.operation", e.operation);
console.log("e.XMLHttpRequest.status", e.XMLHttpRequest.status);
//e.operation is upload or remove
if (e.operation === "upload") {
// a file was added, get the response
var fileid = e.response;
} else {
// Do something after a file was removed
}
}
我的 console.log 条目返回此数据:
控制台日志值
这就是我从服务器返回数据的方式:
public HttpResponseMessage InsertTempFile()
{
HttpPostedFile file = System.Web.HttpContext.Current.Request.Files[0];
//........
// Code that adds my file to the database
// and generates a new primary key for my file
//.........
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(myNewId.ToString());
return response;
}
response.Content 在 e.response 中返回我的新 ID HttpStatusCode.Ok 返回我的状态 200。如果您检查响应,还会返回一堆其他数据。
请注意,要使用 HttpResponseMessage 和 HttpStatuseCode,您需要在类中包含以下命名空间:
using System.Net.Http;
using System.Net;