一:前台使用上传文件标签
<input name="fileUpload" type="file" /></td>
二:后台使用HttpPostedFileBase处理文件
1:获取
HttpPostedFileBase fileUpload = _request.Files[0];
三:配置允许上传文件的最大值
<httpRuntime targetFramework="4.5" maxRequestLength="10485760" executionTimeout="3600"/>
四:用ajaxform上传例子
前台
@{
Layout = null;
}
<!DOCTYPE html>
<script src="~/Content/Plug/jquery-easyui-1.3.5/jquery.min.js"></script>
<script src="~/Content/js/jquery.form.js"></script>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Emcee</title>
</head>
<body>
<div>
<form id="upfrom" enctype="multipart/form-data">
<input name="fileUpload" type="file" />
<input type="button" value="上传" name="上传" onclick="emcee.sc()"/>
</form>
</div>
</body>
</html>
<script>
var emcee = {};
$(function () {
var options = {
type: "post",
url: "../Emcee/Upload",
success: function (data) {
alert(data);
}
};
$('#upfrom').ajaxForm(options);
});
emcee.sc = function ()
{
$('#upfrom').submit();
}
</script>
后台:
[HttpPost]
public JsonResult Upload()
{
try
{
HttpPostedFileBase fileUpload = Request.Files[0];
string path = AppDomain.CurrentDomain.BaseDirectory + "Users/";
if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(path);
}
string filename = Path.GetFileName(Request.Files[0].FileName);
Request.Files[0].SaveAs(Path.Combine(path, filename));
return Json("上传成功!");
}
catch (Exception e) {
return Json("上传失败!");
}
}