jQuery fileupload动态创建表单并发送到s3

时间:2014-05-28 07:32:47

标签: javascript jquery ajax file-upload

我想通过ajax发送表单上传通过jquery-fileupload添加的文件。我的问题是当我试图覆盖我想要的add()函数时:

  1. 为文件创建数据库对象
  2. 创建动态表单
  3. 将fileupload中的一个文件附加到创建的表单
  4. 通过ajax发送到s3

    add: (event, data) ->
    # send each file to the s3
    for file in data.files
        $.ajax({
            url: "/attachments",
            data: { ... }
            type: "POST",
            processData: false,
            dataType: "json",
            async: false,
            success: (retdata) ->
                # now when object in db has been created i want to send a file
                s3Data = retdata
                s3Form = $("<form>", {
                    id: "avatar-form",
                    action:  "#{s3Data.schema}://#{s3Data.host}",
                    method: s3Data.method,
                    enctype: "multipart/form-data",
                    acceptCharset: "UTF-8"
                })
                # create inputs for files
                for key, value of s3Data.formdata
                    s3Form.append("<input type=\"hidden\" name=\"#{key}\" value=\"#{value}\"/> ")
    
                # here i have my file object from the loop and also access to the
                # data.form, which is jquery file upload form
                # how to extract files from form, and append it to the new s3Form
    
                s3Form.ajaxForm({})
                s3Form.submit()
    
  5. 如何将附件转移到一个表格到第二表格?

1 个答案:

答案 0 :(得分:0)

我猜输入fileupload对象可以通过js访问:http://www.w3schools.com/jsref/dom_obj_fileupload.asp但是表单可能需要先附加到文档中。