ant-design-VUE form 表单提交 文件和表单数据

由于项目里需要用到在一个表单里使用文件和内容一起提交到服务端,因此需要进行处理。

前端:Vue2.0 Ant-desing

后端:.net WebAPI 

界面如下:

 

 页面代码:

<template>
  <div class="clearfix" style="background-color:#ffffff;">
<a-form :form="form">
        <a-form-item label="资源名称"  has-feedback>
          <a-input placeholder="请输入资源名称" v-decorator="['title']" />
        </a-form-item>
        <a-form-item label="资源描述"  has-feedback>
          <a-input placeholder="请输入资源描述" v-decorator="['description']" />
        </a-form-item>
        <a-form-item label="资源路径"  has-feedback>
          <a-input placeholder="请输入资源路径" v-decorator="['path']" />
        <a-upload :file-list="fileList"  :before-upload="beforeUpload"
        v-decorator="[
          'upload',
          {
            valuePropName: 'fileList1',
           
          },
        ]"
       
        > getValueFromEvent: normFile,  name="upload"
            <a-button> <a-icon type="upload" /> 请先择文件 </a-button>
        </a-upload>
        </a-form-item>
        <a-form-item label="应用ID" >
          <a-select style="width: 100%" placeholder="请选择应用ID" v-decorator="['projectAppId', {rules: [{ required: true, message: '请选择应用ID!' }]}]">
            <a-select-option v-for="(item,index) in projectAppIdData" :key="index" :value="item.id">{{ item.title }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="资源类型" >
          <a-select style="width: 100%" placeholder="请选择资源类型" v-decorator="['resourceType', {rules: [{ required: true, message: '请选择资源类型!' }]}]">
            <a-select-option v-for="(item,index) in resourceTypeData" :key="index" :value="item.id">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="版本号"  has-feedback>
          <a-input placeholder="请输入版本号" v-decorator="['versionId']" />
        </a-form-item>
      </a-form>
     <a-button
      type="primary"
      
      :loading="uploading"
      style="margin-top: 16px"
      @click="handleSubmit"
    >
      {{ uploading ? 'Uploading' : 'Start Upload' }}
    </a-button>
    :disabled="fileList.length === 0"
  </div>
</template>
<script> 
import { axios } from '@/utils/request'
export default {
  data() {
    return {
      fileList: [],
      fileList1: [],
      uploading: false,
      form: this.$form.createForm(this),
      projectAppIdData:[{id:1,title:'项目1'}],
      resourceTypeData:[{id:1,name:'显示内容'}],
    };
  },
  methods: {
    handleRemove(file) {
        debugger;
      const index = this.fileList.indexOf(file);
      const newFileList = this.fileList.slice();
      newFileList.splice(index, 1);
      this.fileList = newFileList;
    },
    beforeUpload(file) {
        debugger;
      this.fileList = [...this.fileList, file];
      return false;
    },
    ProjectAppResourcePage (parameter) {
        return axios({
            url: '/api/projectappresourcemanager/upload-file',
            method: 'post', 
           // headers: { 'content-type': 'application/x-www-form-urlencoded' },
            data: parameter
        });
    }
    , 
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            console.log('form',values);
            debugger;
            const formData = new FormData();
             this.fileList.forEach(file => {
                 formData.append('upload', file);
             }); 
            // Object.keys(values.upload).map(item => (
            //     formData.append(item, values.upload[item])
            // ));
            for(var v in values){
                 if(v!='upload'){
                console.log(v+'+',values[v]);
                formData.append(v, values[v]);
                }
                }
            debugger;
            this.ProjectAppResourcePage(formData).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', formData)
                this.handleCancel()
              } else {
                this.$message.error('新增失败:' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      }
    
  }
}
</script>

C#后端代码

 /// <summary>
    /// 项目资源管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiDescriptionSettings("11", Name = "ProjectAppResourceManager", Order = 11)]
    public class ProjectAppResourceManager 
   {

       
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<ProjectAppResourceAddResult>> UploadFile(
            [FromForm(Name = "upload")] List<IFormFile> files,
           [FromForm] ProjectAppResourceAdd projectAppResourceAdd
            )
        {
            var ss = files;
            var result = new List<ProjectAppResourceAddResult>();
            
            return result;
        }



}


    public class obj
{
    /// <summary>
    /// 资源名称
    /// </summary>
    public string Title { get; set; }
    /// <summary>
    /// 资源描述
    /// </summary>
    public string Description { get; set; }
    /// <summary>
    /// 资源路径
    /// </summary>
    public string Path { get; set; }
    /// <summary>
    /// 应用ID
    /// </summary>
    public long ProjectAppId { get; set; }
    /// <summary>
    /// 资源类型,参考ResourceTypeEnum
    /// </summary>
    public short ResourceType { get; set; }
    /// <summary>
    /// 当前资源的版本信息
    /// </summary>
    public string VersionId { get; set; }
}

特别注意:

1.上传内容时一定要指定 [FromForm] 不然会报错

[FromForm(Name = upload)] List files,

[FromForm] obj projectAppResourceAdd

不指定 [FromForm] 报错415 Unsupported Media Type

asp.net WebApi RestFul接口 formdata 415 Unsupported Media Type

如下图:

 

 指定formForm后Swagger正确显示

没增加FormForm特性的swagger错误显示

 

2.使用Ant-desing 的 a-upload上传文件需要在before-upload 中拦截获取到文件内容。然后再把文件内容通过 formdata 发送到后台。

<template>
  <div class="clearfix">
    <a-upload :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload">
      <a-button> <a-icon type="upload" /> Select File </a-button>
    </a-upload>
    <a-button
      type="primary"
      :disabled="fileList.length === 0"
      :loading="uploading"
      style="margin-top: 16px"
      @click="handleUpload"
    >
      {{ uploading ? 'Uploading' : 'Start Upload' }}
    </a-button>
  </div>
</template>
<script>
import reqwest from 'reqwest';
export default {
  data() {
    return {
      fileList: [],
      uploading: false,
    };
  },
  methods: {
    handleRemove(file) {
      const index = this.fileList.indexOf(file);
      const newFileList = this.fileList.slice();
      newFileList.splice(index, 1);
      this.fileList = newFileList;
    },
    beforeUpload(file) {
      this.fileList = [...this.fileList, file];
      return false;
    },
    handleUpload() {
      const { fileList } = this;
      const formData = new FormData();
      fileList.forEach(file => {
        formData.append('files[]', file);
      });
      this.uploading = true;

      // You can use any AJAX library you like
      axu({
        url: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
        method: 'post',
        processData: false,
        data: formData,
        success: () => {
          this.fileList = [];
          this.uploading = false;
          this.$message.success('upload successfully.');
        },
        error: () => {
          this.uploading = false;
          this.$message.error('upload failed.');
        },
      });
    },
  },
};
</script>

注意正常的客户端数据内容:

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 ant-design-vue 中提交一个表单,您可以使用 `Form` 组件和 `Form.Item` 组件。以下是一个简单的示例: ```html <template> <a-form ref="form" :model="formData" :rules="formRules" @submit="handleSubmit"> <a-form-item label="Name" :colon="false" prop="name"> <a-input v-model="formData.name" /> </a-form-item> <a-form-item label="Email" :colon="false" prop="email"> <a-input v-model="formData.email" /> </a-form-item> <a-form-item> <a-button type="primary" html-type="submit">Submit</a-button> </a-form-item> </a-form> </template> <script> export default { data() { return { formData: { name: '', email: '', }, formRules: { name: [{ required: true, message: 'Please enter your name', trigger: 'blur' }], email: [ { required: true, message: 'Please enter your email', trigger: 'blur' }, { type: 'email', message: 'Please enter a valid email address', trigger: 'blur' }, ], }, }; }, methods: { handleSubmit() { this.$refs.form.validate(valid => { if (valid) { // Submit the form data console.log(this.formData); } else { console.log('Form validation failed'); return false; } }); }, }, }; </script> ``` 在上面的代码中,我们首先创建了一个 `Form` 组件,并将其绑定到 `formData` 数据属性和 `formRules` 数据属性。然后,我们在表单中添加了两个 `Form.Item` 组件,分别用于输入名称和电子邮件地址。最后,我们添加了一个提交按钮,并将其绑定到 `handleSubmit` 方法。 在 `handleSubmit` 方法中,我们首先使用 `this.$refs.form.validate` 方法验证表单数据。如果表单数据有效,则提交表单数据,否则输出一个错误消息。 请注意,我们在 `a-button` 组件上使用 `html-type="submit"` 属性来指定按钮类型为提交按钮。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值