function createUploadForm(fileElementId, data, curFileList) { var id = new Date().getTime(); var formId = 'jUploadForm' + id; //給form添加一個獨一無二的id var f... ...
function createUploadForm(fileElementId, data, curFileList) { var id = new Date().getTime(); var formId = 'jUploadForm' + id; //給form添加一個獨一無二的id var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data" ></form>'); //創建form元素 if (data) { for (var i in data) { jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form); } } //set attributes jQuery(form).css('position', 'absolute'); //給動態form添加樣式,使其浮動起來, jQuery(form).css('top', '-1200px'); jQuery(form).css('left', '-1200px'); jQuery(form).appendTo('body'); //把動態form插入到body中 var fd = new FormData($('#' + formId)[0]); for (var i = 0; i < curFileList.length; ++i) { fd.append('file[]', curFileList[i]); } return fd; } 使用方法: var fd = createUploadForm("file" + row, { entityName: entityName, entityId: id, userId: userId, listName: rowData.ListName }, curFileList); $.ajax({ url: ClientUrl + '/UploadFile/Upload.html', type: "post", data: fd, dataType: "json", processData: false, contentType: false, success: function (data) { $('#trackRecordTable').bootstrapTable('hideLoading'); $('#trackRecordTable').bootstrapTable('refresh'); alert(data.msg); }, error: function (data) { $('#trackRecordTable').bootstrapTable('hideLoading'); var result = eval('(' + data.responseText + ')'); alert(result.msg); } });