上传 asp.net mvc 2 后对话框不出现

发布于 2024-11-05 20:46:19 字数 3195 浏览 3 评论 0原文

上传成功后我的对话框没有弹出?上传工作正常,但 $("#dialog-confirm").dialog 不起作用?

<h2>
    upload Data</h2>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script src="../../Scripts/jqueryform.js" type="text/javascript"></script>
<script src="../../Scripts/jblock.js" type="text/javascript"></script>

<script src="../../Content/jquery-ui-1.8.12.custom/js/jquery-ui-1.8.12.custom.min.js"
    type="text/javascript"></script>

<link href="../../Content/jquery-ui-1.8.12.custom/css/ui-lightness/jquery-ui-1.8.12.custom.css"
    rel="stylesheet" type="text/css" />



<script type="text/javascript">
    $(function () {
        $("#ajaxUploadForm").ajaxForm({
            iframe: true,
            dataType: "json",
            beforeSubmit: function () {
                $("#ajaxUploadForm").block({ message: '<h1><img src="/Content/busy.gif" /> uploading file...</h1>' });
            },
            success: function (result) {
                $("#ajaxUploadForm").unblock();
                $("#ajaxUploadForm").resetForm();
                $.growlUI(null, result.message);

                //alert(result.message);

                //does not popup??
                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    buttons: {
                        "Ok":
                        function () {
                            alert('ok');
                            $(this).dialog("close");
                        }
                    ,
                        Cancel: function () {
                            $(this).dialog("close");
                        }
                    }
                });


            },
            error: function (xhr, textStatus, errorThrown) {
                $("#ajaxUploadForm").unblock();
                $("#ajaxUploadForm").resetForm();
                $.growlUI(null, 'Error uploading file');
            }
        });
    });
</script>
<form id="ajaxUploadForm" action="<%= Url.Action("AjaxUpload", "Home")%>" method="post"    enctype="multipart/form-data">
<fieldset>
    <legend>Upload a file</legend>
    <label>
        File to upload:
        <input type="file" name="file" />(100MB max size)</label>
    <input id="ajaxUploadButton" type="submit" value="Submit" />
</fieldset>
</form>


  public FileUploadJsonResult AjaxUpload(HttpPostedFileBase file)
    {
        // TODO: Add your business logic here and/or save the file
        System.Threading.Thread.Sleep(2000); // Simulate a long running upload

        // Return JSON
        return new FileUploadJsonResult { Data = new { message = string.Format("{0} uploaded successfully.", System.IO.Path.GetFileName(file.FileName)) } };
    }

my dialog does not popup after successfull upload? the upload works fine but $("#dialog-confirm").dialog does not work?

<h2>
    upload Data</h2>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script src="../../Scripts/jqueryform.js" type="text/javascript"></script>
<script src="../../Scripts/jblock.js" type="text/javascript"></script>

<script src="../../Content/jquery-ui-1.8.12.custom/js/jquery-ui-1.8.12.custom.min.js"
    type="text/javascript"></script>

<link href="../../Content/jquery-ui-1.8.12.custom/css/ui-lightness/jquery-ui-1.8.12.custom.css"
    rel="stylesheet" type="text/css" />



<script type="text/javascript">
    $(function () {
        $("#ajaxUploadForm").ajaxForm({
            iframe: true,
            dataType: "json",
            beforeSubmit: function () {
                $("#ajaxUploadForm").block({ message: '<h1><img src="/Content/busy.gif" /> uploading file...</h1>' });
            },
            success: function (result) {
                $("#ajaxUploadForm").unblock();
                $("#ajaxUploadForm").resetForm();
                $.growlUI(null, result.message);

                //alert(result.message);

                //does not popup??
                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    buttons: {
                        "Ok":
                        function () {
                            alert('ok');
                            $(this).dialog("close");
                        }
                    ,
                        Cancel: function () {
                            $(this).dialog("close");
                        }
                    }
                });


            },
            error: function (xhr, textStatus, errorThrown) {
                $("#ajaxUploadForm").unblock();
                $("#ajaxUploadForm").resetForm();
                $.growlUI(null, 'Error uploading file');
            }
        });
    });
</script>
<form id="ajaxUploadForm" action="<%= Url.Action("AjaxUpload", "Home")%>" method="post"    enctype="multipart/form-data">
<fieldset>
    <legend>Upload a file</legend>
    <label>
        File to upload:
        <input type="file" name="file" />(100MB max size)</label>
    <input id="ajaxUploadButton" type="submit" value="Submit" />
</fieldset>
</form>


  public FileUploadJsonResult AjaxUpload(HttpPostedFileBase file)
    {
        // TODO: Add your business logic here and/or save the file
        System.Threading.Thread.Sleep(2000); // Simulate a long running upload

        // Return JSON
        return new FileUploadJsonResult { Data = new { message = string.Format("{0} uploaded successfully.", System.IO.Path.GetFileName(file.FileName)) } };
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住我的手 2024-11-12 20:46:19

我发现您正在使用 jquery.form 插件来 AJAX 化表单并从控制器操作返回 JSON 。以下是文档对此场景的描述:

由于无法上传
使用浏览器的文件
XMLHttpRequest 对象,表单插件
使用隐藏的 iframe 元素来帮助
与任务。这是一个常见的
技术,但有其内在的
限制。 iframe 元素是
用作表单的目标
提交操作意味着
服务器响应被写入
iframe。如果响应就很好
类型是 HTML 或 XML,但不起作用
如果响应类型是脚本
或 JSON,两者通常都包含
需要重新表达的字符
找到时使用实体引用
HTML 标记。

应对挑战
脚本和 JSON 响应,表单
插件允许这些响应
嵌入到 textarea 元素中
建议您这样做
这些响应类型在使用时
与文件上传结合。

因此,要使其正常工作,服务器的响应需要如下所示:

<textarea>{ message: 'file uploaded successfully' }</textarea>

这是这个自定义 FileUploadJsonResult 在控制器操作中所做的事情吗?

I see that you are using the jquery.form plugin for AJAXifying the form and returning JSON from your controller action. Here's what the documentation says about this scenario:

Since it is not possible to upload
files using the browser's
XMLHttpRequest object, the Form Plugin
uses a hidden iframe element to help
with the task. This is a common
technique, but it has inherent
limitations. The iframe element is
used as the target of the form's
submit operation which means that the
server response is written to the
iframe. This is fine if the response
type is HTML or XML, but doesn't work
as well if the response type is script
or JSON, both of which often contain
characters that need to be repesented
using entity references when found in
HTML markup.

To account for the challenges of
script and JSON responses, the Form
Plugin allows these responses to be
embedded in a textarea element and it
is recommended that you do so for
these response types when used in
conjuction with file uploads.

So for this to work the response from the server needs to look like this:

<textarea>{ message: 'file uploaded successfully' }</textarea>

Is it what this custom FileUploadJsonResult is doing in your controller action?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文