发布:2014/11/13 9:04:13作者:管理员 来源:本站 浏览次数:2388
MVC使用Ajax.BeginForm上传图片时HttpPostedFileBase file为null,Request.Files获取不到文件,问题分析是页面中存在jquery.unobtrusive-ajax.js文件,注释后 能够获取到。改为Html.BeginForm实现如下:
1
2
3
4
5
6
|
@using (Html.BeginForm("UploadPhoto", "EditUserInfo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
选择图片:
< input type = "file" name = "uploadFile" />
< input type = "submit" />
}
|
Controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[HttpPost]
public ActionResult UploadPhoto(HttpPostedFileBase uploadFile)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
{
string fileName = WebSecurity.CurrentUserId + Path.GetExtension(uploadFile.FileName)
string direction = Server.MapPath( "~/UpLoad/Companys/logo/" );
string destinationPath = Path.Combine(direction, fileName);
if (!Directory.Exists(direction))
{
Directory.CreateDirectory(direction);
}
uploadFile.SaveAs(destinationPath);
}
return View();
}
|
© Copyright 2014 - 2025 柏港建站平台 ejk5.com. 渝ICP备16000791号-4