日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

前后端交互,前端參數格式form-data

作者:多樂_π 更新時間: 2023-07-15 編程語言

注意:文件上傳,要不要存在阿里云,要不要永久保存,回顯的時候 需要阿里云,用完就刪除的直接給后端就行,不需要存在阿里云。

1、原生input提交





/** 提交校驗 */
  onCheck = async () => {
    const { idType, defaultValue, getSubmitValue } = this.props;
    const { selectCode } = this.state;


    try {
// 方法一:
      // const formDataObject = new FormData();
      // console.log(($("#file")[0] as any).files, "所有文件")見下面截圖

      // const files = ($("#file")[0] as any).files;
// 同時上傳多個文件時的寫法
      // for (const file of files) {
      //   formDataObject.append('files', file);
      // }
// 方法二:
      const input = document.querySelector("input[type='file']");

//const input = document.querySelector("input[type='file']");
// 等同于
//const input = document.querySelector(".input");
      const formDataObject = new FormData(); // FormData 對象獲取文件數據
// 同時上傳多個文件時的寫法
      for (let i = 0; i < (input as any).files?.length; i++) {
        formDataObject.append("file", (input as any).files[i]);
      }


// 方法三
      const values = (await this.props.form.validateFields()) as unknown as TFarmLand;
      values.deptId = !isEmpty(selectCode) ? selectCode?.map(Number)[selectCode.length - 1] : defaultValue?.deptId as number;
      const data = new FormData();
      const formDataObject = idType === "edit" ? data : formData;
      formDataObject.append("name", values.name || "");

      
        await tFarmLandProvider.addFarmForm(formDataObject).then(async (res) => {
          if (res.code === 200) {
            this.props.getSubmitValue && this.props.getSubmitValue(true);
            message.success("新增成功!");
          }
          return;
        }).catch(err => {
          this.props.getSubmitValue && this.props.getSubmitValue(false);
          message.error(err.response?.data?.msg || "新增失敗!");
          return;
        });
   

    } catch (errorInfo) {
      console.log(errorInfo, "errorInfo")
      message.warn("提交校驗失敗!");
      return null;
    }
      
  }


// 方法三
 /** 獲取formdata */
  getBeforeUpload = (file: RcFile) => {
    console.log(file, "file")
    const data = new FormData();
    data.append("file", file);
    this.setState({
      formData: data,
    })
    return false;
  }


render() {
   
    return (
      // 方法一:
      // <div id="uploadForm">
      //   <input id="file" type="file" name="file" multiple/>
      // </div>
      // 方法二:
      //<form id="form_upload" encType="multipart/form-data">
      //  <input className="input" type="file" name="file" multiple />
      //</form>
      // 方法三:
<Form className="farm">
<Form.Item label="農田名稱">
          {getFieldDecorator("name", {
            initialValue: defaultValue?.name,
          })(
            <Input
              placeholder="請輸入"
            />)}
        </Form.Item>
        {<Form.Item label="農田文件上傳" extra="請上傳農田shp格式文件壓縮包" style={{ display: idType === "edit" ? "none" : "" }}>
          {getFieldDecorator("file", {
            validateTrigger: "onSubmit",
            rules: [
              {
                required: idType === "edit" ? false : true,
                message: "文件必須上傳!",
              },
              { validator: checkedUploadSuccess },
            ],
            valuePropName: "file",
            getValueFromEvent: (e: UploadChangeParam) => {
              return e.fileList;
            },
          })(<Upload
            name="file"
            multiple
            transformFile={transformFile}
            accept=".zip"
            beforeUpload={(file) => this.getBeforeUpload(file)}
            listType="picture"
          >
            <Button>點擊上傳</Button>
          </Upload>)}
        </Form.Item>}

      </Form>

    )
  }

?同時上傳多個的時候,就會并列給2個file: binary

?所有文件打印:

原文鏈接:https://blog.csdn.net/weixin_45481456/article/details/131474837

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新