【发布时间】: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