简体   繁体   English

Kendo UI上传未返回onSuccess

[英]Kendo UI Upload not returning onSuccess

Kendo UI Upload does not seem to return onSuccess even though the file gets uploaded. 即使文件已上传,Kendo UI Upload似乎也不会返回onSuccess。 Below is the code I am using in C# on page load. 下面是页面加载时我在C#中使用的代码。 I need to file name to be returned 我需要返回文件名

What am I doing wrong here? 我在这里做错了什么?

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 of success code 成功代码的Javascript

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

Edit 1 编辑1

I found that entire html seems to be rendered after uploading. 我发现整个HTML似乎在上传后呈现。 How do I just return the uniquefilename? 我该如何返回唯一文件名?

Update 更新

What I did to resolve this issue was call another aspx file that had no html and got the correct response. 解决该问题的方法是调用另一个没有html并获得正确响应的aspx文件。 Not sure if this is the correct way but works for me. 不知道这是否正确,但是对我有用。 Now just need to find out how to get the unique file name back. 现在只需找出如何获取唯一文件名即可。

My Code: 我的代码:

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

Server Code: 服务器代码:

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

    }

For future reference, 备查,

You should be using a WebService (ASMX) file to process and return your JSON. 您应该使用WebService(ASMX)文件来处理和返回JSON。 Remember that the javascript success block will only be called with a returned http 200 status code. 请记住,仅会使用返回的http 200状态代码来调用javascript成功块。

If you ever get to using the MVC framework its even easier by just returning a JSON result type. 如果您曾经使用过MVC框架,则只需返回JSON结果类型就可以更加轻松地进行操作。

If you wanted to keep the aspx hack, you can call the following to remove the HTML 如果您想保留aspx hack,可以调用以下命令删除HTML

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

But again i would discourage from doing this and recommend the dedicated service. 但是我再次不鼓励这样做,并推荐专用服务。

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

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