JQuery - 在文件上传中将变量附加到表单的formdata

时间:2016-04-05 02:28:27

标签: javascript jquery ajax

我试图上传文件。问题是我想在表单中添加一个JSON变量。 我将需要该变量到我的php文件。

这是我的HTML:

<div id = "progress-div-closeoutfile"><div id="progress-bar-closeoutfile"></div></div>
 <form method="POST" id='closeoutfile_form' class = "upload_form" action = "{{ url('/store-files-closeout') }}">
   <input type="hidden" class="form-control" id = "closeoutfile_hidden" value = "none">

   <input type="hidden" name="_token" value="{{ csrf_token() }}">
   <label>Closeout file:</label>
   <input id="closeoutfile_input" type="file" name="closeoutfile_input[]" class="form-control file_upload" style="margin-right:10px;">

   <button type="submit" style="color:gray; margin-top:10px; margin-right:5px;" class="btn btn-default btn-sm pull-right save_btn" id = "closeoutfile_save" disabled> Save &nbsp; <i class="fa fa-floppy-o"></i></button>      
 </form>

这是我的jquery AJAX

$('.upload_form').on('submit',function(e){
        id_value = $(this).attr('id').replace('_form','');
        formdata = new FormData($(this));
        formdata.append('id_value',JSON.stringify(id_value));  // i want to add this variable
        $(this).ajaxSubmit({
            beforeSubmit: function() {
                $("#progress-bar-"+id_value).width('0%');
            },
            uploadProgress: function(event, position, total, percentComplete) {
                $("#progress-bar-"+id_value).width(percentComplete + '%');
                $("#progress-bar-"+id_value).html('<div id="progress-status">' + percentComplete + ' % </div>')
            },
            success: function() {

            }
        });
    });

0 个答案:

没有答案