如何在liferay中使用合金ui ajax调用发送文件和数据

时间:2015-02-16 12:53:59

标签: liferay alloy-ui

我有以下ALLOY UI脚本的代码片段:

AUI().use('aui-base','aui-io-request', function(A){          
         A.io.request('<%=submitJobApplicationByAjax%>',{
             dataType: 'json',
             method: 'POST',
             data:{                  
                "appliedPosition": A.one("#<portlet:namespace/>appliedPosition").val(),
                "fullName" : A.one("#<portlet:namespace/>fullName").val(),


             },
             on: {
             success: function() {

                 }
             }
         });
    });

以下表格:

<input class="span12"  type="text" name="<portlet:namespace/>fullName" id="<portlet:namespace/>fullName"                            value="${fullName}" required="required" pattern=".*\S+.*" />
<input class="span12 file_up_btn" type="file"                               name="<portlet:namespace/>uploadResume" required="required"                             id="<portlet:namespace/>uploadRes" onclick="<portlet:namespace/>clearErrorMsg()" />

如何通过alloy ui中的liferay ajax调用发送文件和数据。

2 个答案:

答案 0 :(得分:1)

您可以使用FormData通过AJAX发送文件,

var file_data = $("#fileName").prop("files")[0];
// Creating object of FormData class
var formData = new FormData();
// Appending parameter named file with properties of file_field to form_data
formData.append("fileName", file_data);
formData.append("appliedPosition", "someValue");
formData.append("fullName", "someValue");
A.io.request('<%=submitJobApplicationByAjax%>',{
         dataType: 'json',
         method: 'POST',
         data: formData,
         .........
         .........

但是,此解决方案不适用于IE9。

注意:如果您有兴趣使用AUI,也可以查看 AUI Liferay.Upload

答案 1 :(得分:0)

在您使用的代码中

A.one("#<portlet:namespace/>appliedPosition").val()

获取appliedPosition值。

正确的代码是使用

A.one("#<portlet:namespace/>appliedPosition").get("value")

使用alloyui时。

此外,您可以阅读以下内容。

您可以使用以下网址了解详情。

http://yuilibrary.com/yui/docs/io/#uploading-files-in-an-html-form

我们可以将这段代码直接放在我们的合金代码中。

AUI()。ready('module-name1','modulename2',function(){

/*
 * This example demonstrates how to configure io to upload files
 * from an HTML form.  This example uses the global events:
 * "io:start" and "io:complete" to handle the transaction and
 * response.  Transaction events can be defined and fired, as well,
 * in the configuration object; but, they are not used in this
 * example.
 */
// Create a YUI instance using the io-upload-iframe sub-module.
YUI().use("io-upload-iframe", function(Y) {
    // Create a configuration object for the file upload transaction.
    // The form configuration should include two defined properties:
    // id: This can be the ID or an object reference to the HTML form
    //     containing the input type="file" elements.
    // upload: Set this property to "true" to indicate this is a file
    //         upload transaction.
    var cfg = {
        method: 'POST',
        form: {
            id: formObject,
            upload: true
        }
    };

    // Define a function to handle the start of a transaction
    function start(id, args) {
      var id = id; // Transaction ID.
      var args = args.foo; // 'bar'
    }

    // Define a function to handle the response data.
    function complete(id, o, args) {
      var id = id; // Transaction ID.
      var data = o.responseText; // Response data.
      var args = args[1]; // 'ipsum'.
    };

    // Subscribe to event "io:start", and pass an object
    // as an argument to the event handler "start".
    Y.on('io:start', start, Y, { 'foo':'bar' });

    // Subscribe to event "io:complete", and pass an array
    // as an argument to the event handler "complete".
    Y.on('io:complete', complete, Y, ['lorem', 'ipsum']);

    // Start the transaction.
    var request = Y.io(uri, cfg);
});

});

我相信我们也可以使用A.io.request。 你也应该设置

enctype: "multipart/form-data" 

在io请求的配置中。