【问题标题】:Kendo UI FileUpload via AJAX with JSONKendo UI FileUpload 通过 AJAX 和 JSON
【发布时间】:2014-02-09 16:22:03
【问题描述】:

我在 MVC 5 中使用 Kendo。我有几个表单输入,包括一个文件上传控件。单击按钮我正在构建一个带有输入值的 json 对象,并通过 AJAX 调用发送它。

我想知道如何在发送的 json 对象中包含从文件上传控件中选择的文件。这是上传控件的代码:

$(document).ready(function () {
    $("#files").kendoUpload({
        multiple: false
    });
});

然后ajax调用发送表单数据:

var ro = new Object();
// ...... //populate some properties      
var jsonString = JSON.stringify(ro);

$.ajax({
    url: '@Url.Action("Save", "Service")',
    data: jsonString ,
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
});

接收控制器动作如下所示:

public ActionResult Save(MyViewModel model)
{
    var obj = //call something here then return resulting obj 
    return this.Json(obj, JsonRequestBehavior.AllowGet);
}  

感谢任何帮助。

【问题讨论】:

  • @cmedine- 你能做到吗?你能发布解决方案吗
  • 我无法让它工作并实施了一个临时存储解决方案,该解决方案在应用重启时被清除。

标签: ajax json file-upload kendo-ui


【解决方案1】:

试试

 $("#files").kendoUpload({
            multiple: false,
            async: {
                saveUrl: '@Url.Action("Save", "Service")',
                autoUpload: true
            },
            upload: function (e) {
                var ro = new Object();
                //......//populate some properties      

                var jsonString = JSON.stringify(ro);
                e.data = jsonString;
            }
        });

在您的控制器中:

public ActionResult Save(IEnumerable<HttpPostedFileBase> files, string model) 
            // string because you used JSON.stringify and it is a string, do not stringify, if you need an object
        {
            var js = new JavaScriptSerializer();
            var objetModel = js.Deserialize<MyViewModel>(model);            

            var obj = //call something here then return resulting obj 

            return Json(obj, JsonRequestBehavior.AllowGet);

        }

我没有测试它 - 这只是为了让您了解如何将附加信息与上传的二进制文件一起传递

【讨论】:

  • 谢谢,但是上传控件没有使用异步 - 在他们选择提交之前我不想上传任何东西
猜你喜欢
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多