【问题标题】:Kendo UI Upload not returning onSuccess剑道 UI 上传不返回 onSuccess
【发布时间】:2014-08-05 09:43:51
【问题描述】:

即使文件已上传,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。如何只返回唯一文件名?

更新

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

    }

【问题讨论】:

    标签: c# javascript kendo-ui


    【解决方案1】:

    供日后参考,

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

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

    如果您想保留 aspx hack,您可以调用以下命令来删除 HTML

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

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

    【讨论】:

    • 谢谢。我终于转向了比 aspx 更容易的 MVC。
    猜你喜欢
    • 2012-08-11
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多