Form action 方法上传文件

<form method="post" id="form1" runat="server" enctype="multipart/form-data" action="@Url.Action("/Upload")" >
<ul>
<li><label> 版本号:</label><input type="text" id="txtVersion" name="nVersion" /></li>
<li><label> 说明:</label><input type="text" id="txtRemark" name="nRemark" /></li>
<li> <label> 文件:</label> <input type="file" id="vfile" name="upload" /> </li>
<li><label></label> <input type="submit" id="submit" value="提交" /></li>
</ul>
</form>

<script type="text/javascript">
$(function () {
var tips = '@ViewBag.Msg';
if (!!tips) {
alert(tips);
}
})
</script>

#region 上传APK
[HttpGet]
public ActionResult Upload()
{
return View();
}

[HttpPost]
public ActionResult Upload(string id)
{
if (Request.ContentLength > 0 && Request.Files.Count > 0)
{
try
{
StringBuilder sb = new StringBuilder();
string version = Request.Form["nVersion"].ToString();
string remark = Request.Form["nRemark"].ToString();
if (string.IsNullOrEmpty(version))
{
sb.Append("请填写版本号!");
}
else
{
bool isVer = true;
Version v = new Version();
try
{
v = new Version(version);
}
catch
{
sb.Append("请输入正确的版本号!");
isVer = false;
}
if (isVer)
{
HttpPostedFileBase fb = Request.Files[0];
if (fb.ContentLength > 0)
{
string fileName = fb.FileName;
int extIndex = fileName.LastIndexOf(".");
string extension = fileName.Substring(extIndex).ToLower();
if (extension == ".gif")
{
if (!Directory.Exists(Server.MapPath("~/File")))
{
Directory.CreateDirectory(Server.MapPath("~/File"));
}

string fileSavePath = Server.MapPath("~/File") + "\" + fileName;
string filenewname = fileName;
if (System.IO.File.Exists(fileSavePath))
{
filenewname = fileName.Substring(0, extIndex) + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
fileSavePath = Server.MapPath("~/File") + "\" + filenewname;
}

fb.SaveAs(fileSavePath);
string newUrl = ConfigurationManager.AppSettings["GetApkPath"] + "/File/" + filenewname;

if (sysversion.AddVersion(v.ToString(), newUrl, remark))
{
sb.Append("上传成功!");
}
}
else
{
sb.Append("请上传gif格式的文件!");
}
}
else
{
sb.Append("请上传gif格式的文件!");
}
}
}
ViewBag.Msg = buffer.ToString();
}
catch (Exception e)
{
throw e;
}
}
return View("Upload");
}
#endregion

原文地址:https://www.cnblogs.com/jcz1206/p/3464663.html