简体   繁体   English

Telerik asp.net MVC文件上传控件

[英]Telerik asp.net MVC Fileupload control

I am using Telerik asp.net MVC 3 file control in my Razor view ( Catalog / Product View) like this: 我在Razor视图( Catalog / Product视图)中使用Telerik asp.net MVC 3文件控件,如下所示:

@(Html.Telerik().Upload()
    .Name("orderImageAtachment")
    .Async(async => async.Save("Save", "Catalog").AutoUpload(true))
    .ClientEvents(events => events
        .OnSuccess("ItemImageOnSuccess")
        .OnError("ItemImageOnError")
    )
)

I have created an ActionResult like this: 我创建了一个这样的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("");
        }

and client side functions like this: 和客户端功能是这样的:

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

I get select button which opens browse window. 我得到选择按钮,它将打开浏览窗口。 But clicking it does nothing.... I am not sure whats wrong. 但是单击它不会执行任何操作。 Do I need to add something in web.config? 我需要在web.config中添加一些内容吗? Please suggest. 请提出建议。

I'm a little confused at which point its not working, but I'm assuming its not hitting the action in your controller. 我有点困惑,这时它不起作用,但是我假设它没有在您的控制器中起作用。 I'd make sure you are trying a fairly small file, the default limit is 4mb. 我会确保您尝试的文件非常小,默认限制为4mb。

Also it looks like the signature of your Save Action does not match the route you are giving it in the upload's async.Save(...) . 而且看起来您的“ Save操作”的签名与您在上传的async.Save(...)中给出的路由不匹配。 I'm not sure it will matter since its a string, but you might try removing the Save actions's CompID parameter (doesn't look like its used in the snippet at least). 我不确定这是否重要,因为它是一个字符串,但是您可以尝试删除Save操作的CompID参数(至少看起来与代码段中使用的参数不同)。

I'd try using fiddler or the developer tools in whichever browser you are using to see if u are getting a 404 error by chance. 无论您使用哪种浏览器,我都会尝试使用提琴手或开发人员工具,以查看您是否偶然遇到404错误。

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

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