Kendo UI上传未返回onSuccess

时间:2014-04-14 05:16:11

标签: c# javascript kendo-ui

即使文件上传,Kendo UI Upload似乎也没有返回onSuccess。下面是我在C#页面加载时使用的代码。我需要提交要返回的文件名

我在这里做错了什么?

public string ProcessRequest()
{

        Response.Expires = -1;
        try
        {

            HttpPostedFile postedFile = Request.Files["files"];
            string savepath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["ExcelImport"]);
            string filename = CL.GenerateUniqueName(postedFile.FileName, Session["LoginUserID"].ToString());  \\ <== generate a unique file name and return it to upload
            if (!Directory.Exists(savepath))
                Directory.CreateDirectory(savepath);
            string strCompath = Path.Combine(savepath, filename);
            postedFile.SaveAs(strCompath);
            Response.ContentType = "text/plain";
            string json = JsonConvert.SerializeObject(filename, Formatting.Indented); //<== tried to convert to json but still does not work
            return "";
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            return ex.ToString();
        }
 }

成功代码的Javascript

function onSuccess(e) {
                console.log("Success (" + e.operation + ") :: " + getFileInfo(e));
}

修改1

我发现整个html似乎在上传后呈现。我如何只返回uniquefilename?

更新

我为解决此问题所做的是调用另一个没有html的aspx文件,并得到正确的响应。不确定这是否是正确的方法,但对我有用。现在只需要了解如何获取唯一的文件名。

我的代码:

$("#files").kendoUpload({
   async: {
           saveUrl: "Response.aspx",  //<== earlier this was pointing to the current aspx where the control is
           removeUrl: "remove",
           autoUpload: true
          },
             showFileList: true,
             success: onSuccess,
             remove: onRemove,
             upload: onUpload,
             complete: onComplete,
             error: onerror
   });

服务器代码:

 protected void Page_Load(object sender, EventArgs e)
 {

           Response.Expires = -1;
            try
            {

                HttpPostedFile postedFile = Request.Files["files"];
                string savepath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["ExcelImport"]);
                string filename = CL.GenerateUniqueName(postedFile.FileName, Session["LoginUserID"].ToString());
                if (!Directory.Exists(savepath))
                    Directory.CreateDirectory(savepath);
                string strCompath = Path.Combine(savepath, filename);
                postedFile.SaveAs(strCompath);
                Context.Response.Clear();
                Context.Response.ContentType = "text/plain";
                Context.Response.Write("{}");

            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
                // return ex.ToString();
            }

    }

1 个答案:

答案 0 :(得分:1)

供将来参考,

您应该使用WebService(ASMX)文件来处理和返回您的JSON。请记住,只会使用返回的http 200状态代码调用javascript成功块。

如果您使用MVC框架,只需返回JSON结果类型即可。

如果您想保留aspx hack,可以调用以下内容删除HTML

response.clear();
response.write(yourJSON);
response.end();

但我再次不鼓励这样做,并推荐专门的服务。