1. html
<form id="form_search" action="@Url.Action("UpLoadFile")" enctype="multipart/form-data" method="post">
<td>
上传文件:<input class="easyui-filebox" id="fileTxt" name="fileName" style=" 200px" data-options="prompt:'Choose a file...'" />
<a id="UpLoadFile" class="easyui-linkbutton" style="" data-options="iconCls:'icon-fresh',70" onclick="upLoadFile()">
上传
</a>
</td>
<script>
$('#fileTxt').filebox({
buttonText: '选择文件',
buttonAlign: 'right'
})
</script>
//文件上传
function upLoadFile() {
$("#UpLoadFile").click(function () {
debugger;
var upfile = $("#fileTxt").filebox("getValue");
if (upfile.length <= 0) {
$.mapuni.tips("请选择要上传的文件");
return;
}
var ext = upfile.split('.');
//$("#searchForm_dg").submit()
$("#form_search").ajaxSubmit(function (message) {
alert(message);
window.location.reload();
});
});
}
3.后台
public string UpLoadFile()
{
HttpPostedFileBase file = Request.Files["fileName"];
string fileName = Path.GetFileName(file.FileName);
string fileExt = Path.GetExtension(fileName);
if (fileExt == ".xls" || fileExt == ".xlsx")
{
//string dir=excelPath
Directory.CreateDirectory(Path.GetDirectoryName(excelPath));
string fullDir = excelPath + fileName;
file.SaveAs(fullDir);
return "文件保存成功";
}
else
{
return "文件保存失败";
}
}
private string excelPath = ConfigurationManager.AppSettings["DailyPublicationFilePath"];//"D:\Test\";//上传到服务器的路径
4.配置文件 AppSettingConfig.xml
<!--上传excel存放路径-->
<add key="ExcelFilePath" value="D:HeNan" />