我有一个ASP。NET MVC应用程序。此应用程序使用引导文件输入插件。上传文件后,我需要检查返回的JSON。此时,我的代码结构如下:
Index.cshtml
<input id="myPictures" name="files" type="file" multiple>
...
$('#myPictures').on('filebatchuploadcomplete', function(event, files, extra)
{
console.log(event);
console.log(files);
console.log(extra);
});
$("#myPictures").fileinput({
maxFileCount: 1,
overwriteInitial: false,
uploadUrl: "/Pictures/Upload",
uploadAsync: true,
previewSettings: {
image: {width: "200px", height: "200px"}
}
});
图片控制器.cs
public class PicturesController
{
[HttpPost]
public ActionResult Upload(FormCollection form)
{
// Do stuff
var resultId = GetResultId();
return Json(new { id = pictureId }, JsonRequestBehavior.AllowGet);
}
}
我的问题是,当返回JSON时,我似乎无法在filebatchuploadcomplete
事件处理程序中访问它。我已经确认Upload
操作正在通过使用断点和Fiddler返回我期望的JSON。然而,我不知道如何读取响应以根据服务器的结果做出反应。
有人能帮我弄清楚如何读取从服务器返回的JSON对象中id
属性的值吗?它把我逼疯了!
感谢
您不能使用正在使用的事件来执行此操作,因为它会提供响应。请尝试成功事件。
$('#myPictures').on('filebatchuploadsuccess', function(event, data, previewId, index) {
console.log(data.response); // id is in data.response
});
如果您使用filebatchuploadcomplete而不是success来处理错误,则可能必须使用特定的错误事件单独处理这些错误。