c# - Kendo UI 上传不返回 onSuccess

标签 c# javascript kendo-ui

即使文件已上传,Kendo UI 上传似乎也不会返回 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 都被渲染了。如何只返回唯一文件名?

更新

我为解决这个问题所做的是调用另一个没有 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();
            }

    }

最佳答案

供将来引用,

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

如果您曾经使用过 MVC 框架,只需返回一个 JSON 结果类型就更容易了。

如果你想保留 aspx hack,你可以调用下面的代码来删除 HTML

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

但我再次劝阻不要这样做并推荐专门的服务。

关于c# - Kendo UI 上传不返回 onSuccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23052441/

相关文章:

javascript - 在 Angular-Kendo Treeview 中,使用类名隐藏/显示跨度元素

c# - 您如何获得 WCF 性能指标?

c# - 浮点到其他格式的字节数组

javascript - 从数组中获取随机数,不重复(使用计时器)

javascript - 在页面重新加载时恢复 &lt;textarea&gt; 滚动条位置

javascript - 为什么我的按钮每次都不起作用,为什么它不打印前 6 个 FIRST?

asp.net-mvc - 同一页面中的 2 kendo ui Treeview 未扩展节点

javascript - Kendo UI 颜色选择器不显示选择颜色的对话框

c# - goto - 不在范围内 (C#)

c# - 如何检查 DateTime.Now 是否仅在时间部分的两个给定 DateTime 之间?