上传文件(ajax结合form表单)

1、 form直接提交

<form method="post" action="url"  enctype="multipart/form-data" target="hideframe">             
    <input type="file" name="file" value="导入">
    <input type="submit" value="确定" >
</form>
<iframe name="hideframe" style="display:none;"></iframe>

2、ajax提交

<input type="file">

// 点击确认,提交
var formData = new FormData();

formData.append('file', $('input[type=file]')[0].files[0]);    // 'file'表示提交时的键名
var jqXHR = $.ajax({
    type: 'post',
    data: formData,
    dataType: 'json',
    url: 'url',
    processData: false,  // 告诉jQuery不要去处理发送的数据
    contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
});
原文地址:https://www.cnblogs.com/Zting00/p/7497672.html