zoukankan      html  css  js  c++  java
  • ASP.NET MVC 上传文件方法

    前端:
    
    <form id="form" method="post" action="/Attachment/Save/" enctype="multipart/form-data" >
    
      <table> 
    
    <tr>
                <td>
                   上传文件:
                </td>
                <td>
               
                  <input id="FileNamePath" required="true" name="FileNamePath" type="file" />(注:选择图片文件)
                </td>
            </tr>
    
    
        </table>
        </form>

    这里要特别注意:enctype="multipart/form-data",否则后台无法获取到

    后端:
    string fileExtension = string.Empty; ;
    string filename = string.Empty; ;
    string filePathName = string.Empty;
     long fileSize = 0;
    if (Request.Files["FileNamePath"].ContentLength>0)
    {
                    string path = Server.MapPath(ConfigHelp.GetAppSettings("ADImage"));
                    string uploadFileName = Path.GetFileName(Request.Files["FileNamePath"].FileName);
    
                      fileExtension = System.IO.Path.GetExtension(Request.Files["FileNamePath"].FileName).ToLower();
                    filename = entity.Title + fileExtension;
                    filePathName = Path.Combine(path, filename);
                    Request.Files["FileNamePath"].SaveAs(filePathName);
                    fileSize = Request.Files["FileNamePath"].ContentLength;
    
    }
    

     多个文件时后台的获取方式为:

          foreach(string name in Request.Files)
           {
             Request.Files["name"]
         }

    这里的name是前台标签input的name

  • 相关阅读:
    第六课 使用oflash软件烧写bin文件至开发板
    Linux查看、添加、修改PATH环境变量
    第七课 Linux裸机开发+SourceInsight3.5使用+notepad++使用
    第五课 Linux高级命令
    数组的方法总结
    浅谈 return false 和preventDefault stopPropagation stopImmediatePropagation 的正确用法
    实时统计输入的文字
    滚轮滚动事件
    window.onload和DOMReady
    JS获取浏览器可视区域的尺寸
  • 原文地址:https://www.cnblogs.com/f2flow/p/2866608.html
Copyright © 2011-2022 走看看