如何获取从kendoui上传成功或完成函数返回的值

问题描述:

我正在使用Kendo UI上传控件。我已经定义了剑道UI上传这样的:如何获取从kendoui上传成功或完成函数返回的值

<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); 
} 

返回代码后,我想要检索的功能齐全的代码。我怎样才能做到这一点?

+0

你试图'的console.log(E)'看到所返回什么?我愿意成为'e'不是'事件',而是'从srever返回的数据'。如果您使用'var data = $ .parseJSON(e)',您最终可能会得到一个数据对象,该数据对象具有您的控制器定义的属性。 – Ohgodwhy 2013-03-20 06:50:55

+0

Yah数据变量包含类似于“服务器响应:实际字符串”的字符串。 – Pa1 2013-03-20 07:09:11

+0

被返回的项目是一个对象,你使用parseJSON,它不是你发送的对象?在这里发布对象,使用jsfiddle并保存它。 – Ohgodwhy 2013-03-20 07:10:10

你可以得到的回应像这样

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

成功函数,这些函数返回JSON可能是

return json(new { data=text, "text/plain" }); 
+0

请参阅nukefusion的答案。 'e.response。data()'不起作用。 – 8protons 2017-02-17 00:40:11

+0

响应的Content-Type应该是'text/plain'以将数据返回给事件处理程序,包括返回JSON对象。任何其他内容类型将被视为错误。 'e.response.data()'应该是'e.response.data',因为响应应该是一个对象。 要从控制器返回包含'data'属性的对象,代码应该是'return Json(new {data = text},“text/plain”);'。 – Suncat2000 2017-05-10 13:21:46

如果你只是传递一个字符串返回,你应该能够做到:

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; 
} 
+0

我们只需要返回文本/纯文本。我们可以发送其他格式,如html或xhtml – Pa1 2013-03-20 13:56:41

我会在此处添加我的答案以及其他有效答案。首先,虽然你会希望获得成功的功能,而不是完整的功能返回的响应:

 $("#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 
     }); 

的成功函数返回一个对象(通常人们将其命名为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 
    } 
} 

我的控制台.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返回我的新的ID在e.response 的HttpStatusCode.Ok返回我的200状态有,如果你检查响应时返回,以及其他数据的一群。

注意使用HttpResponseMessage和HttpStatuseCode你需要在你的类下面的命名空间:

using System.Net.Http; 
using System.Net;