一,view代码
<form role="form"><div class="form-group"><label for="keyinput">选择文件:</label><input type="file" name="upfile" id="upfile" /></div><div class="form-group"><input class="btn btn-minw btn-danger" type="button" οnclick="importExp()" value="ajax导入" /></div> </form>
function importExp() {var formData = new FormData();formData.append("file", document.getElementById("upfile").files[0]);$.ajax({type: 'POST',url: '/s/Import',data: formData,cache: false,contentType: false,processData: false,success: function (data) {layer.msg(data.msg);// 清空input file文件原生jsvar file = document.getElementById("upfile");// for IE, Opera, Safari, Chromeif (file.outerHTML) {file.outerHTML = file.outerHTML;} else { // FF(包括3.5)file.value = "";}}});}
二,控制器代码
public JsonResult ImportExecl(){bool isOk = false;var file = Request.Files[0];if (file == null || file.ContentLength <= 0){return Json(new { IsOk = isOk, msg = "请上传excel文件" });}if (!new List<string>() { ".xls" }.Contains(Path.GetExtension(file.FileName))){return Json(new { IsOk = isOk, msg = "请上传格式为.xls的excel文件" });}var a = file.InputStream;}
综上,就可以获取到file.InputStream文件流,然后对文件流的操作就可以了