0

いくつかのパラメーターをasp.netmvc2アプリケーションに渡そうとしています。jqueryformプラグインを使用しています。ファイルをアップロードするときにそれぞれ異なるタイプを渡す3つのリンクがあります。後でサーバーのqueystringから読み取られる隠しフィールドを使用しています。これを試しましたが、リクエストが投稿されませんか?

<script type="text/javascript">

    $(document).ready(function () {
        function subm() {
            $('#fileUploadForm').ajaxForm({
                url: "/Home/Upload/",
                method: "POST",
                beforeSubmit: ShowRequest,
                success: SubmitSuccesful,
                error: AjaxError
            });
        }

        function ShowRequest(formData, jqForm, options) {
            var queryString = $.param(formData);
            alert('BeforeSend method: \n\nAbout to submit: \n\n' + queryString);
            return true;
        }

        function AjaxError() {
            alert("An AJAX error occured.");
        }

        function SubmitSuccesful(responseText, statusText) {
            alert("SuccesMethod:\n\n" + responseText);
        }

        $("#uploadLink").click(function () {
            // set type
            $("input[name=hiddenField]").val("Type1");
            subm();
        })



    });

</script>
<body>
    <form id="fileUploadForm" action="" enctype="multipart/form-data">
    <input type="text" name="filename" />
    <input type="file" id="postedFile" name="postedFile" />

    <input type="hidden" name="hiddenField" />
    <a id="uploadLink">upload type 1</a> <a id="uploadLink2">upload type 2</a> <a id="uploadLink3">
        upload type 3</a>
    </form>
</body>

 public FileUploadJsonResult Upload(HttpPostedFileBase postedFile)
    {
        var type = Request.QueryString["hiddenField"];
        return new FileUploadJsonResult { Data = new { message = "success" } };
    }

public class FileUploadJsonResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        this.ContentType = "text/html";
        context.HttpContext.Response.Write("<textarea>");
        base.ExecuteResult(context);
        context.HttpContext.Response.Write("</textarea>");
    }
}
4

1 に答える 1

0

ajaxFormAJAX リクエストを送信しません。フォームを AJAX 化します。AJAX 送信を強制する場合は、次を使用する必要がありますajaxSubmit

$(function() {
    // AJAXify the form
    $('#fileUploadForm').ajaxForm({
        url: '/Home/Upload/',
        method: 'POST',
        beforeSubmit: ShowRequest,
        success: SubmitSuccesful,
        error: AjaxError
     });
});

// the next functions could be declared outside of the document.ready handler

function subm() {
    $('#fileUploadForm').ajaxSubmit();
}

function ShowRequest(formData, jqForm, options) {
    var queryString = $.param(formData);
    alert('BeforeSend method: \n\nAbout to submit: \n\n' + queryString);
    return true;
}

function AjaxError() {
    alert("An AJAX error occured.");
}

function SubmitSuccesful(responseText, statusText) {
    alert("SuccesMethod:\n\n" + responseText);
}

$("#uploadLink").click(function () {
    // set type
    $("input[name=hiddenField]").val("Type1");
    subm();
    return false;
});

また、AJAX リクエストの URL とメソッドをハードコーディングするのはなぜですか。フォームを作成するときは、次のようにします。

<% using (Html.BeginForm("Update", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "fileUploadForm" })) { %>
    <input type="file" id="postedFile" name="postedFile" />
    <input type="hidden" name="hiddenField" />
    <a id="uploadLink">upload type 1</a> 
    <a id="uploadLink2">upload type 2</a> 
    <a id="uploadLink3">upload type 3</a>
<% } %>

そして簡単に:

$(function() {
    $('#fileUploadForm').ajaxForm({
        beforeSubmit: ShowRequest,
        success: SubmitSuccesful,
        error: AjaxError
     });
});

これにより、ホストされている場所に関係なく、アプリケーションが確実に機能します。行ったように URL をハードコーディングし、IIS の仮想ディレクトリに展開すると、URL が無効になるためです。

于 2011-07-06T05:14:53.130 に答える