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

  • 相关阅读:
    java实现同步的两种方式
    JAVA线程概念
    XML基础总结
    JAVA使用和操作properties文件
    JAVA序列化基础知识
    easyui 在编辑状态下,动态修改其他列值。
    Activiti初学问题,求解
    java web--DOM
    java web(1)
    Java WEB
  • 原文地址:https://www.cnblogs.com/f2flow/p/2866608.html
Copyright © 2011-2022 走看看