MVC3 上传文件
前臺引擎采用Razor
上傳頁View:
@model System.Web.HttpContextBase@{
ViewBag.Title = "上傳文件";
}
<h2>上傳文件</h2>
<br />
<br />
@*new { enctype = "multipart/form-data" }比不可少,否則上傳文件不會成功 *@
@using (Html.BeginForm("Upload", "UploadFile", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<text>選擇上傳文件:</text><input name="file" type="file" id="file" />
<br />
<br />
<input type="submit" name="Upload" value="Upload" />
}
你的后臺UploadFileController:
[HttpPost]public ActionResult Upload(FormCollection form)
{
if (Request.Files.Count == 0)
{
//Request.Files.Count 文件數(shù)為0上傳不成功
Return View();
}
var file = Request.Files[0];
if (file.ContentLength == 0)
{
//文件大小大(以字節(jié)為單位)為0時(shí),做一些操作
Return View();
}
else
{
//文件大小不為0
HttpPostedFileBase file = Request.Files[0];
//保存成自己的文件全路徑,newfile就是你上傳后保存的文件,
//服務(wù)器上的UpLoadFile文件夾必須有讀寫權(quán)限
file.SaveAs(Server.MapPath(@"UploadFile\newfile"));
}
newFile = DateTime.Now.ToString("yyyyMMddHHmmss") + ".sl";
return View();
}
好了,上傳后的文件就可以任你操作了
轉(zhuǎn)載于:https://www.cnblogs.com/leiOOlei/archive/2011/08/17/2143221.html
總結(jié)
- 上一篇: window自带字体
- 下一篇: 如何处理Global symbol *