1

上传文件后,在将其保存到磁盘之前,我正在做一些验证。

如果出现问题,我想向用户显示我的自定义错误消息。我在这里发现了一些奇怪的东西Uploadify: show error message from HTTP response

如何在 MVC 3 中做到这一点?

4

2 回答 2

2

Tony,

Taken straight from ab mvc3 app that I'm working on right now:

<script type="text/javascript">
    function initupLoadify() {
        $("#fileInput").uploadify({
            uploader: '@Url.Content("~/scripts/swf/uploadify.swf")',
            script: '@Url.Action("Upload", "Home")',
            cancelImg: '@Url.Content("~/Content/cancel.png")',
            auto: true,
            sizeLimit: 5500000,
            fileDataName: 'fileData',
            //scriptData: { 'propertyId': $("#PropertyID").val() },
            buttonText: 'Add Schedule',
            wmode: 'transparent',
            //buttonImg: '@Url.Content("~/Content/button.png")', // make nice gradient image for button
            onComplete: function (event, queueId, fileObj, response) {
                $("#msg").html(response);
                // do something
                setTimeout(2000, window.location = '@Url.Action("Index")' + '?id=' + response);
                return true;
            },
            onCancel: function (evt, queueId, fileObj, data) {
                $("#msg").html("<br />Operation cancelled");
                return true;
            },
            onOpen: function (evt, queueId, fileObj) {
                $("#msg").html("<br />Upload in progress");
                return true;
            },
            onError: function (event, queueId, fileObj, errorObj) {
                $("#msg").html(fileObj.name + " was not uploaded ");
                if (errorObj.status == 404)
                    $("#msg").html("Could not find upload script. Use a path relative to: " + "<?= getcwd() ?>");
                else if (errorObj.type === "HTTP")
                    $("#msg").html("error " + errorObj.type + ": " + errorObj.status);
                else if (errorObj.type === "File Size")
                    $("#msg").html(fileObj.name + " " + errorObj.type + " Limit: " + errorObj.info / 1000 + " KB");
                else
                    $("#msg").html("error " + errorObj.type + ": " + errorObj.text);
            }
        });
    };
</script>

hope this helps

于 2012-07-20T10:57:25.720 回答
0

这是一个 MVC 3示例

如果您使用的是 3.0+ 版本,请使用onUploadComplete而不是 onComplete(意识到它们的参数不同)。

于 2012-11-15T11:44:10.700 回答