Html
<input type= "file" Name= "Excel" Id= "Excel_input" accept= ". Doc,.docx,.xls,.xlsx" onchange = "uploadfile (th is,1) "/>
Js
function Getfiletype (FilePath) {
Gets the suffix name of the file
var startIndex = Filepath.lastindexof (".");
if (startIndex! =-1)
Return filepath.substring (startindex+1, filepath.length);
else return "";
}
function UploadFile (obj, type) {
var FilePath = $ ("#excel_input"). Val ();
if (""! = FilePath) {
var fileType = Getfiletype (FilePath);
Determine if the uploaded attachment is a Word file and an Excel file
if ("Doc"!=filetype && "docx"!=filetype && "xls"!=filetype && "xlsx"!=filetype) {
$ ("#excel_input"). Val ("");
Alert ("Please upload form file");
}
else{
Get attachment size (in KB)
var fileSize = document.getElementById ("Excel_input"). files[0].size/1024;
if (FileSize > 500) {
Alert ("File size cannot exceed 500KB");
$ ("#excel_input"). Val ("");
} else{
var formData = new FormData ();
var name = $ ("#excel_input"). Val ();
Formdata.append ("Excel", $ ("#excel_input") [0].files[0]); Get the contents of a file
Formdata.append ("name", name); Path to the file
$.ajax ({
Type: ' POST ',
Processdata:false,//does not process the sent data because the data value is a Formdata object and does not need to be processed
Contenttype:false,//Do not set Content-type request header
URL: "/admin/user/addusers",//fill in your own way
Data:formdata,
DataType: ' json ',//return value type is generally set to JSON
Success:function (data) {//server successful response handler function
Alert ("Upload success");
Window.location.reload ();//Refresh the page after uploading successfully
},
Error:function (data) {
Alert ("Server Exception");
}
});
}
}
}
return false;
}
HTML Ajax upload file Limits file type and file size