繁体   English   中英

Kendo UI上传未返回onSuccess

[英]Kendo UI Upload not returning onSuccess

即使文件已上传,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();
            }

    }

备查,

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

如果您曾经使用过MVC框架,则只需返回JSON结果类型就可以更加轻松地进行操作。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM