.net mvc4 怎么用<input type="file" id='file_id" />来发送小于100MB的文件?不用控件?谢谢! 20
1个回答
展开全部
<input type="file" id='file_id" onclick=Save() />
<script>
window.location="/控制器/Save";
</script>
在控制器写代码:
public ActionResult Save(IEnumerable<HttpPostedFileBase> NewsURL)
{
var physicalPath = "";
if (NewsURL != null)
{
foreach (var file in NewsURL)
{
var fileName = Path.GetFileName(file.FileName);
//判断上传的文件类型
string extentionName = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - (fileName.LastIndexOf(".") + 1));
//生成新的文件名
Random rand = new Random();
string newFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Millisecond.ToString() + rand.Next().ToString() + "." + extentionName;
physicalPath = Path.Combine(Server.MapPath("~/images"), newFileName);
file.SaveAs(physicalPath);
url = "/images/" + newFileName;
}
}
return Content("");
}
这样就把文件存入到images文件夹中了
<script>
window.location="/控制器/Save";
</script>
在控制器写代码:
public ActionResult Save(IEnumerable<HttpPostedFileBase> NewsURL)
{
var physicalPath = "";
if (NewsURL != null)
{
foreach (var file in NewsURL)
{
var fileName = Path.GetFileName(file.FileName);
//判断上传的文件类型
string extentionName = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - (fileName.LastIndexOf(".") + 1));
//生成新的文件名
Random rand = new Random();
string newFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Millisecond.ToString() + rand.Next().ToString() + "." + extentionName;
physicalPath = Path.Combine(Server.MapPath("~/images"), newFileName);
file.SaveAs(physicalPath);
url = "/images/" + newFileName;
}
}
return Content("");
}
这样就把文件存入到images文件夹中了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询