[英]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.