上傳報告文檔限制為pdf,word,excel文件,通過input的accept屬性設置
<div class="inputs" style="width:100%;"> <input type="file" accept="*.pdf,*.doc,*.xls" id="crowd_file_tz"/> <input type="button" class="upfileBtn" id="uploadFileTZ" value="上傳"/> </div>
通過ajax發送請求:
//上傳台賬信息 $('#uploadFileTZ').click(function(){ uploadFile('4','#crowd_file_tz'); }); var uploadFile = function(file_type,fileDom){ var formData = new FormData(); var albh = sbbm; var crowdFile = $(fileDom)[0].files[0]; formData.append('file',crowdFile); formData.append('albh',albh); formData.append('tplx',file_type); $.ajax({ url: '/insertAltpAndWd', type: 'POST', cache: false, //上傳文件不需要緩存 data: formData, processData: false, // 告訴jQuery不要去處理發送的數據 contentType: false, // 告訴jQuery不要去設置Content-Type請求頭 success: function (data) { console.log(data); MsgBox('提示','插入成功'); setTimeout(fadeOut,500); }, error: function (data) { MsgBox('提示','上傳失敗'); setTimeout(fadeOut,500); } }) }