• MVC认知路【点点滴滴支离破碎】【五】----form表单上传单个文件


    //个人理解:前台一个form加input[type='file'],在加一个submit的按钮

       主要设置form的action,method,enctype='multipart/form-data'

       * file要指定 name要不后台Request不到额,

       后台:Request.files[]

               file.FileName

               Request.MapPath("")

               file.SaveAs("")

        /*  一系列业务操作  */

    贴两行代码

    前台

            <form id="formUpload" action="/Hunter/Upload" method="post" enctype="multipart/form-data">
                <table border="0" cellspacing="0" cellpadding="0" class="tableTdP5 tableMarCen">
                    <tr>
                        <td class="gray-9">上传简历</td>
                        <td>
                            <div class="fileBox">
                                <input type="text" class="inp-bor">
                                <span class="btn">浏览</span>
                                <input type="file" name="file" id="file" value="" class="file" />
                            </div>
                        </td>
                        <td>
                            <input type="submit" class="baseBtn btn-lar" value="上传" />
                        </td>
                    </tr>
                </table>
            </form>

    后台

    public void Upload()
            {
                HttpFileCollectionBase files = Request.Files;
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFileBase file = files[i];
                    if (file.ContentLength == 0)
                    {
                        Response.Redirect("/url/Error");
                    }
                    //获取后缀名
                    string houzhui = file.FileName.Substring(file.FileName.LastIndexOf("."));
                    string newFileName = Guid.NewGuid() + Path.GetFileName(file.FileName);
                    var saveFilePath = Path.Combine(Request.MapPath("~/Upload"), newFileName);
                    try
                    {
                        file.SaveAs(saveFilePath);
                        SaveP_Resume(newFileName);
                    }
                    catch
                    {
                        Response.Redirect("url");
                    }
                }
            }
  • 相关阅读:
    超实用的JavaScript代码段 Item1 --倒计时效果
    你不知道的JavaScript--Item23 定时器的合理使用
    你不知道的JavaScript--Item22 Date对象全解析
    解决使用adb卸载应用失败的问题
    使用adb命令安装安卓apk包
    软件测试理论2
    软件测试理论1
    将Django部署到服务器(下)
    MySQL基本语句
    Nginx&uWSGI
  • 原文地址:https://www.cnblogs.com/oiliu/p/4661473.html
Copyright © 2020-2023  润新知