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

  • 相关阅读:
    java架构师学习目录 sany
    python学习字符串 sany
    python中os.open()和open()区别 sany
    python3学习列表 sany
    C语言博客园作业03
    c语言博客作业02
    程序员竞争力列表
    《程序员》三月刊摘要
    Storage Systems IMPACT 课程结束
    deployJava.js的一个缺憾:无法正确检测客户端JRE
  • 原文地址:https://www.cnblogs.com/jcz1206/p/3464663.html
Copyright © 2011-2022 走看看