【问题标题】:How To Use OnSubmit with Dropzone?如何在 Dropzone 中使用 OnSubmit?
【发布时间】:2020-11-23 12:23:34
【问题描述】:

我正在使用 dropzone 将图像上传到 amazon s3,但我希望在按下提交按钮后上传图像,而不是在“丢弃”图像时上传。我怎么能这样做?这是我的代码。每当我尝试删除图像时,我都会出错:Unhandled Rejection (TypeError): event.preventDefault is not a function

import React, { useState } from 'react'
import { Typography, Button, Form, message, Input, Icon } from 'antd';
import FileUpload from '../../utils/FileUpload'
import Axios from 'axios';
import Dropzone from 'react-dropzone';
import download from './download.jpeg'



const { Title } = Typography;
const { TextArea } = Input;

 
const Continents = [
    { key: 1, value: "1" },
    { key: 2, value: "2" },
    { key: 3, value: "3" },
    { key: 4, value: "4" },
    { key: 5, value: "5" },
    { key: 6, value: "6" },
    { key: 7, value: "7" }
]


function UploadProductPage(props) {

    const [TitleValue, setTitleValue] = useState("")
    const [DescriptionValue, setDescriptionValue] = useState("")
    const [PriceValue, setPriceValue] = useState(0)
    const [ContinentValue, setContinentValue] = useState([])
    const [Images, setImages] = useState([])

    const onTitleChange = (event) => {
        setTitleValue(event.currentTarget.value)
    }

    const onDescriptionChange = (event) => {
        setDescriptionValue(event.currentTarget.value)
    }

    const onPriceChange = (event) => {
        setPriceValue(event.currentTarget.value)
    }

    const onContinentsSelectChange = (event) => {
        setContinentValue(event.currentTarget.value)
    }

    const updateImages = (newImages) => {
        setImages(newImages)
    }


    ///////// on drop stuff 

  
    const onSubmit = (event, files) => {
        event.preventDefault();


        if (!TitleValue || !DescriptionValue || !PriceValue ||
            !ContinentValue || !Images) {
            return alert('fill all the fields first!')
        }

        const variables = {
            writer: props.user.userData._id,
            title: TitleValue,
            description: DescriptionValue,
            price: PriceValue,
            images: Images,
            continents: ContinentValue,
        }

        Axios.post('/api/product/uploadProduct', variables)
            .then(response => {
                if (response.data.success) {
                    alert('Product Successfully Uploaded')
                    props.history.push({
                        pathname: "/user/cart",
                        state: {
                          data: variables,
                        }, 
                      })
                } else {
                    alert('Failed to upload Product')
                }
            })
            
            let formData = new FormData();
            const config = {
                header: {
                    'accept': 'application/json',
                    'Accept-Language': 'en-US,en;q=0.8',
                    'Content-Type': `multipart/form-data; boundary=${formData._boundary}`
                }
            }
            formData.append("galleryImage", files)
            //save the Image we chose inside the Node Server 
            Axios.post('/api/photo/uploadMultiple', formData, config)
                .then(response => {
                    if (response.data.success) {
    
                        setImages([...Images, response.data.image])
                        props.refreshFunction([...Images, response.data.image])
    
                    } else {
                        alert('Failed to save the Image in Server')
                    }
                })
    }

    return (
        
        <div style={{ maxWidth: '700px', margin: '2rem auto' }}>
            <div style={{ textAlign: 'center', marginBottom: '2rem' }}>
                <Title level={2}> Upload Prompt </Title>

    
            </div>
            <div style={{ display: 'flex', justifyContent: 'space-between' }}>
            

        </div>
            


            <Form onSubmit={onSubmit} >

                {/* DropZone */}
                <div >
             <Dropzone
                onDrop={onSubmit}
                multiple={false}
                maxSize={800000000}
            >
                {({ getRootProps, getInputProps }) => (
                    <div style={{
                        width: '300px', height: '240px', border: '1px solid lightgray',
                        display: 'flex', alignItems: 'center', justifyContent: 'center'
                    }}
                        {...getRootProps()}
                    >
                        {console.log('getRootProps', { ...getRootProps() })}
                        {console.log('getInputProps', { ...getInputProps() })}
                        <input {...getInputProps()} />
                        <Icon type="plus" style={{ fontSize: '3rem' }} />

                    </div>
                )}
            </Dropzone>
        </div>

                <br />
                <br />
                <label>Subject</label>
                <Input
                    onChange={onTitleChange}
                    value={TitleValue}
                />
                <br />
                <br />
                <label>Title</label>
                <Input
                    onChange={onDescriptionChange}
                    value={DescriptionValue}
                />
                <br />
                <br />
                <label>Pages</label>
                <Input
                    onChange={onPriceChange}
                    value={PriceValue}
                    type="number"
                />
                <br /><br />
                <label>Due Date</label>
                <br /><br />
                <Input onChange={onContinentsSelectChange} value={ContinentValue}
                    type="date"
                />
                <br />
                <br />
                <Button
                    onClick={onSubmit}
                >
                    Submit
                </Button>
            </Form>
        </div>
    )
}

export default UploadProductPage

【问题讨论】:

    标签: javascript reactjs dropzone


    【解决方案1】:

    我今天刚刚遇到同样的问题 - 对于未来的任何人,这里是 react-dropzone-uploader 的解决方案......只需查找 react-dropzone 的道具,因为它们可能非常相似,所以这仍然适用(评论后注意到差异库 - 翻白眼)

    1. 使用submitButtonContent 属性并创建您自己的按钮
    2. 使用回调来访问事件
    3. 使用e.preventDefault() 或类似的东西...

    我无法通过“react-dropzone-uploader”配置标准的原始道具访问事件

    submitButtonContent={() => (
        <button onClick={(e) => {
           e.preventDefault();
                                                        
                                                        
           console.log("SUBMIT!");
       }} className="btn btn-secondary" style={{ width: "100%" }}>Submit File</button>
    )}
    

    类似的东西! (注意返回括号)

    【讨论】:

      猜你喜欢
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 2021-10-14
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多