GithubHelp home page GithubHelp logo

Comments (2)

Dishock avatar Dishock commented on May 24, 2024

This is not a Strapi issue, nor plugin issue.

  1. When creating Products collection in Strapi, make sure to name it products, not product. Otherwise you'll face inconsistences.

  2. Provide user permission to create products in Strapi settings. Also provide them permission to upload.

  3. Your input field should look as:
    <input type="file" id="thumbnail" name="files" @change="onFileChange" accept="image/*">

  4. Your onFileChange should look like this

        onFileChange(e) {
            var files = e.target.files || e.dataTransfer.files;
            if (!files.length)
                return;
            this.thumbnail = files;
        },
  1. Create this function, right after the onFilesChange.
        addFiles(collection, id, column, files) {
            var data = new FormData();
            data.append('ref', collection);
            data.append('refId', id);
            data.append('field', column);
            for(var i=0; i<files.length; i++) {
                data.append('files', files[i]);
            }
            return this.$strapi.create('upload', data);
        },
  1. Then I would design your createNew function as such:
        const response = await this.$strapi.create('listings', {
                title: this.title, 
                description: this.description
            })
            .then(res => {
                const response = this.addFiles('listings', res.id, 'thumbnail', this.thumbnail)
                .then(res => {
                    console.log(res)
                })
                .catch(err => {
                    console.log(err)
                })
            .catch(err => {
                    console.log(err);
            });
        }

And you should be done. Works with both multiple files and single files.

If you still have problems, I suggest using Insomnia to play with API endpoints. Have a nice life!

from strapi.

Dishock avatar Dishock commented on May 24, 2024

Thank you, me! :)

from strapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.