MVC:上传文件

今天写了一个使用MVC上传的DEMO,很简单不超过10行代码。代码如下(关注重点,所以尽量精简掉其他代码):

  • 项目结构

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARAAAAEoCAIAAAAfQTmCAAAYxUlEQVR4nO2dz4sc55nH87eEVMmTtYOtCcski2zMrGRY29UhbsmHTTB4IYwDPoRuWcFk5yB8GOLkWkEwm4OJRRaB0R7ey+wlgsanwMaiwPdhGIQEgmZPQQu9h7f6/fm8VfV2VVe9b9d3+CB6eqqqSz3vZ973rX6/9Xxn/96qIf/2X6u//99K/Wq4Y5KkJ9985iJNr+ztvcxZLBZJktpHWKxWq+Xq5/zB+ernj1er1eruvdWXy/IBf2Z1ru1193y1Wq0Wj7Qj7H+1Ol+tVudy3/172mPjFemTUTYuX1r9OpebrZTj8PP58ivn/25fOZkvv1qf6vpH/IXOH6+fV/7j54+1k+Hf8uOIry+/MjeWL6Se7Up/0/j/1HpF+/+727zxxo1XXnn1lVde/U7DHd77z9X//t18oxruy4XhSvAH6rdewvA2d74sf0myoa9/o0bTES1bbZTqr7wrYcTrajzSmlSVMOs2qjZN7fH6JBeP9OcfaU1W/I3gZ8VfS7yu8WdlsZLbqI+J/6lb0ZHgLUwbOuxhRBPkzUtt6MRfPuX3aggg/qDWCmO3D0OYfevP813lPG1h+LfqYe0zL19u3UzNg+siLaht7GPKLst+3kcY+v+76/QtzIdffPD2p5nRw7z9afbhFx+8dGXPQxi9vZoN3WimK/2vqdpjPNJ+2S2FMRqiKYx4XbHZuXnYu+r+5/phl6sv1z8tG7TefPf1Fmw+ud693Hd9YuePiSFZQ2GI/++u07cwL7+xf/fr47c/zYQwb3+a3f36+OU39pMkXShfpDCbIZqRPQqKA30OAwakb2G4Mx9+8YEYiX34xQfcFpvB351QgDDB0KswgiybCCuybDL4uxA6ECYYhhEGgEiBMAB4IIUhZxEAABUIA4AHEAYAD/oTZjq92eHRbt16/+HDh/wTm9u3bw/+PoKR0J8wT5486cqZW7feF59vnp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VUexxRMmWysjybCleRmdrlj+mhWCWUQAP0J09AWF+SQLLFWyjiPoIx4ltrdwBpM+u28sfKpTsGY2cPINDK5mV3umNiMKKE8dFsBSSxryVyT/sFPDIyNOIRJqMvKg58SGCHRCANACEAYADyAMAB4AGEA8ADCAOBBfMIgogwGJEphEFEGQ7E7wmzNmR6qIm/lnJcos7wF4hMm8ayH3Jr4hEGZ5e0RojAd10NuS3zCoGrs9ghUmC7rIbcFwgBJWMJssx5yw5hxapU4VoTh28vRTsW+RlUju2wymRhznUPqWEyNMst9E5wwW6iH7BszNv48K6kVtR6YY1+qbh5VbJkIKgtQZjlcghNmC/WQfWLGRJ5ZRGLM9l2zr/iWLrbsTlOizHLAhCUMp+t6yD4xY4cwy6LQLjc12VcVxtk6laCyvaPzGZRZHoxAhem0HrJvzNgocawJVv6ofl9jSGYVW7aDyiizHAMhCuPCnvrbX9SOXjFj+1ZMagfFx1FsVr+vPrGmrhCYQWWUWY6BWIXxYaDrwiEPe0I+t7CBMB0ScinjkM8tJmISZlN67GFCLmUc8rnFwxiEAaAzIAwAHkAYADyAMAB4EJ8wiCiDAUEVZQA8GHkV5Wp6uB6NLHFkxFdFOfGMKBv17ozqEZXFJLYuDLLE0RFHFeVWEWVtGYi5OrgynLh1YRCNjI44qii3iihrf7znbMkYU9ftVvxdhzDAJI4qyl1ElNMkSZMZW7K5PhAilgOvG7E72yxBlnhcxFFFuV1EWU4VZmydPBHRrnJ6Q6WIndlmAbLEoyOOKsrtIsrCELXUa5FnSnulU8RV2eZyL2SJR0YcVZSTVhHltKw+KTqWsqtRJjB0u+xEGGSJd4o4qihzNo0opwk3hDFtsMQY02+DZKaIXdlmZIlHTDRVlJPNI8ppkpSDLvMGYmppYiJF7Mg2I0s8YmJaS7Zp4jJmMCQLDAgTGsgSB01MwowFZIkDBsIA4AGEAcADCAOABxAGAA+EMP8PBGrRREQrdK4AAAAASUVORK5CYII=" alt="" />

  • 控制器代码
    public ActionResult Index()
    {
    return View();
    } [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file)
    {
    if (file == null)
    {
    return Content("没有文件!");
    } var filename = Path.Combine(Request.MapPath("~/upload"), Path.GetFileName(file.FileName));
    file.SaveAs(filename);
    return Content("OK");
    }
  • 视图代码
    @{
    Layout = null;
    } <!DOCTYPE html> <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    </head>
    <body>
    <div>
    <form enctype="multipart/form-data" method="post" action="uploadfile/Upload">
    <input type="file" name="file" />
    <button type="submit">上传</button>
    </form>
    </div>
    </body>
    </html>
  • 一些值得注意的地方:
    • input标签的name要和update方法的参数的名字一致。
上一篇:Python3.7.1学习(七)mysql中pymysql模块详解(一)


下一篇:小白的Python之路 day5 os,sys模块详解