【问题标题】:How to get the value that is returned from the kendoui upload success or complete function如何获取剑道上传成功或完成函数返回的值
【发布时间】:2013-03-09 03:55:53
【问题描述】:

我正在使用 Kendo UI 上传控件。我已经这样定义了 Kendo UI 上传:

<input type="file" name="resume" />
$("#file").kendoUpload({
    async: {
         saveUrl: "/Home/SaveResume",             
         autoUpload: true
         },            
         complete: function (e)
         {
            // here i want to get the text that is returned from the controller
         }
});

控制器代码如下:

public ActionResult SaveResume(HttpPostedFileBase resume)
{
    var text;
    // code for the file to convert to text and assign it to text
    return Json(text, JsonRequestBehavior.AllowGet);
}

返回代码后,我想检索完整函数中的代码。我该怎么做?

【问题讨论】:

  • 你有没有试过console.log(e)看看返回了什么?我愿意成为e 不是event,而是data returned from the srever。如果您使用var data = $.parseJSON(e),您最终可能会得到一个具有控制器定义的属性的数据对象。
  • 是的,数据变量包含像Server Response: the actual string这样的字符串。
  • 被返回的项目is 一个对象,你使用了 parseJSON 并且它不是你发送的对象?在此处发布对象,使用 jsfiddle 并保存。
  • 解析后我得到了像Server Resonse这样的数据:数据。我现在如何获取实际的字符串。
  • 此 Kendo UI 帮助主题描述了如何从保存处理程序中检索数据:docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/…

标签: c# jquery asp.net-mvc kendo-ui


【解决方案1】:

你可以像这样得到成功函数的响应

function onSuccess(e)
{
    var response = e.response.data();
}

返回的 json 可能在哪里

return Json(new { data = text }, "text/plain");

【讨论】:

  • 请参阅 nukefusion 的回答。 e.response.data() 不起作用。
  • 响应的 Content-Type 应为text/plain 以将数据返回给事件处理程序,包括返回 JSON 对象。任何其他内容类型都将被视为错误。 e.response.data() 应该是 e.response.data,因为响应应该是一个对象。要从控制器返回包含data 属性的对象,代码应为return Json(new { data = text }, "text/plain");
【解决方案2】:

如果你只是传回一个字符串,你应该可以这样做:

function onSuccess(e) {
    var text = e.XMLHttpRequest.responseText;
}

如果需要,您还可以传回更复杂的对象:

// Object
public class MyObject
{
    public int ID { get; set; }
    public string Text { get; set; }
}

// Controller Action
public virtual ActionResult Upload(HttpPostedFileBase file)
{
    return this.Json(new MyObject(), "text/plain");
}

// Javascript Handler
function onSuccess(e) {
    var response = jQuery.parseJSON(e.XMLHttpRequest.responseText);
    var id = response.ID;
    var text = response.Text;
}

【讨论】:

  • 我们只需要返回文本/纯文本。我们可以发送其他格式,如 html 或 xhtml
【解决方案3】:

我将在此处将我的答案与其他有效答案一起添加。首先,尽管您希望在成功函数而不是完整函数中获得返回的响应:

        $("#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 条目返回此数据:

console.log values

这就是我从服务器返回数据的方式:

    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;

【讨论】:

    猜你喜欢
    • 2018-09-02
    • 2015-01-14
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多