zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    禁止进入activity自动弹出键盘
    sqlite的limit使用
    关于anroid设置webview背景方法探讨(转)
    遍历ListView,查出每一项的内容
    虚拟机网络不通故障解决
    zabbix第一篇:zabbix安装及使用
    ansible使用1
    PS1修改xshell命令行样式
    手动配置网卡配置文件ifcfg-eth0
    Linux虚拟机centos系统安装
  • 原文地址:https://www.cnblogs.com/jcz1206/p/3464663.html
Copyright © 2011-2022 走看看