0

Tried to strip down a file upload to a very simple example and break out all the data with a FileReader and FormData so I know what I'm working with before posting to an endpoint. Still failing to see where this data is going, why it is not available on formData.

import axios from 'axios';


export const MyUploadPerfectAssetForm = () => {

    const onChange = (event: any) => {

        const file = event.target.files[0];
        const reader = new FileReader();

        reader.onload = (e) => {
            console.log("logging e.target.result" + e.target?.result)
        };

        //shows the files values properly
        reader.readAsText(file);

        console.log("onChange ran!")

        const formData = new FormData;

        formData.append('file', file);

        console.log("logging formData after append " JSON.stringify(formData))



        const uploadFile = async () => await axios.post("https://myvalidapi-api.myplace.com/asset/upload", formData, {
            headers: {
                'content-type': 'multipart/form-data'
            }
        }).then((response) => { console.log("logging response from postFile" + JSON.stringify(response.data)) })

        uploadFile()
    }



    return (
        <div>
            <form >
                <div>
                    <input id="file"
                        name="test"
                        type="file"
                        onChange={onChange}
                    ></input>
                </div>
                <div>
                    <button type="submit">Submit</button></div>
            </form>
        </div >
    );
}
elderlyman
  • 300
  • 1
  • 10
  • Maybe this will help you https://stackoverflow.com/questions/43013858/how-to-post-a-file-from-a-form-with-axios ? For example the form attribute that specifies multipart/form-data – GoWiser Aug 28 '21 at 00:12

0 Answers0