js方法实现--上传文件功能js实现
程序员文章站
2024-01-17 12:16:46
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);
}
});
上一篇: Python程序员编程10大原则,请牢牢记住,避免吃亏
下一篇: nodejs基础之多进程实例详解