Let's go

表单enctype属性传值问题

form表单的enctype设置为multipart/form-data后,表单中除了文件后台能拿到,其他值后台都拿不到。

知识点:

一、application/x-www-form-urlencoded:

1、表单中的enctype值如果不设置,则默认是application/x-www-form-urlencoded,它会将表单中的数据变为键值对

的形式

2、如果action为get,则将表单数据编码为(name1=value1&name2=value2…),然后把这个字符串加到url后面,中间

用?分隔

3、如果action为post,浏览器把form数据封装到http body中,然后发送到服务器。

二、text/plain:

表单以纯文本形式进行编码

三、multipart/form-data

1、当我们上传的含有非文本内容,即含有文件(txt、MP3等)的时候,需要将form的enctype设置为multipart/form-

data。

四丶Html.BeginForm

1、指定表单提交方式和路径等

 @using (Html.BeginForm("Index", "Home", FormMethod.Get, new { name = "nbform", id = "nbform" }))

2、指定表单提交为数据方式

 @using (Html.BeginForm("ImportExcel", "Stock", FormMethod.Post, new { enctype = "multipart/form-data" }))

 注意, 有时候要加{id=1}不然在点击超过第一页的索引时form后面会自动加上当前页的索引,如果此时再搜索则可能会出来“超出索引值”的错误提示
       @using (Html.BeginForm("Index", null, new { id = 1 }, FormMethod.Get))

3、下面的操作可以防止提交链接后面自带参数

@using (Html.BeginForm("AddDIYBillOfLading", "BillOfLading", new { id ="" }, FormMethod.Post, new { name = "myform", id = "myform" }))

即,RouteValues的id=""

五丶accept属性实现对文件类型的过滤

    在文件上传控件(input type='file')中,可以通过accept属性实现对文件类型的过滤。

    1、相关代码

    下面给出对应的文件上传控件代码:

<input type="file" id="myFile" accept="image/jpeg,image/gif,application/msword"/><br/>

    上面的代码表示限制文件上传类型为.jpg/.gif/.doc。

    2、限制规则

    在文件上传控件的accept属性中,接受以下两种格式的限制符:

  • 文件类型,但不限制具体扩展名,如:image/*,就只要是图片就行。
  • 文件类型+具体的文件扩展类型,如:image/jpeg,使用的是MIME_TYPE类型
  • 多个MIME_TYPE类型之间用逗号分隔。

    3、accept属性具体接受的类型

    以下是几个常用的类型,其它的可以查询相关资料:

  • .doc               application/msword
  • .jpg                image/jpg
  • .txt                text/plain
  • .xls                application/vnd.ms-excel
posted @ 2018-07-31 15:27  chenze  阅读(231)  评论(0编辑  收藏  举报
有事您Q我