我在Razor视图中使用Telerik asp.net MVC 3文件控件(Catalog
/ Product
查看),如下所示:
@(Html.Telerik().Upload()
.Name("orderImageAtachment")
.Async(async => async.Save("Save", "Catalog").AutoUpload(true))
.ClientEvents(events => events
.OnSuccess("ItemImageOnSuccess")
.OnError("ItemImageOnError")
)
)
我已经创建了ActionResult
,如下所示:
public ActionResult Save(IEnumerable<HttpPostedFileBase> orderImageAtachment, string CompID)
{
// The Name of the Upload component is "attachments"
foreach (var file in orderImageAtachment)
{
// Some browsers send file names with full path. This needs to be stripped.
var fileName = Path.GetFileName(file.FileName);
var physicalPath = Path.Combine(Server.MapPath("~/Content/Docs"), fileName);
// The files are not actually saved in this demo
file.SaveAs(physicalPath);
}
// Return an empty string to signify success
return Content("");
}
和客户端功能如下:
function onSuccess(e) {
// Array with information about the uploaded files
var files = e.files;
if (e.operation == "upload") {
alert("Successfully uploaded " + files.length + " files");
}
}
function onError(e) {
alert('Error in file upload');
// Array with information about the uploaded files
var files = e.files;
if (e.operation == "upload") {
alert("Failed to uploaded " + files.length + " files");
}
// Suppress the default error message
e.preventDefault();
}
我得到选择按钮,打开浏览窗口。但是点击它什么也没做......我不确定什么是错的。我需要在web.config中添加一些东西吗?请建议。
答案 0 :(得分:0)
我有点困惑,它在哪一点上不起作用,但我假设它没有在你的控制器中击中动作。我确保你正在尝试一个相当小的文件,默认限制是4mb。
此外,Save
操作的签名与您在上传async.Save(...)
中提供的路线不符。我不确定它是否重要,因为它是一个字符串,但您可能会尝试删除保存操作的CompID
参数(看起来不像它在代码段中使用的那样)。
我会尝试在您使用的任何浏览器中使用fiddler或开发人员工具来查看您是否偶然发现404错误。