0

Im sorry to ask this because there may be lot of tutorials for multiple file upload in django. but none of them clarified my doubts. Im new to Django please be patient with me.

Below is my django codes:

models.py

class Upload(models.Model):
    file = models.FileField(upload_to='images/', default=False)

urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('my_app.urls')),
]

urls.py

urlpatterns = [
    path('image', UploadViewSet, name='image'),
 ]

views.py

class UploadViewSet(viewsets.ModelViewSet):
    queryset = Upload.objects.all()
    permission_classes = [
        permissions.AllowAny
    ]
    serializer_class = UploadSerializer

React

class Design extends Component {
state = {
    file: null
}
handleFile(e) {
    let file = e.target.files[0]
    this.setState({ file: file })
}
handleUpload() {

    let file = this.state.file
    let formdata = new FormData()
    formdata.append('images', file)
    console.log('this one:', file)
    axios({
        url: "http://127.0.0.1:8000/image/",
        method: "POST",
        data: formdata,
    }
    ).then(res => console.log(res))
}

render() {
    return (
        <div class="upload">
            <h1>Upload Images</h1>
            <br />
            <br />

            <Link to="/">
                <button class="btn btn-warning">Back</button>
            </Link>

            <br />
            <br />
            <br />
            <div className="d-flex p-4 z-depth-2">
                <input type="file" multiple name="file" onChange={(e) => this.handleFile(e)} />
            </div>
            <br />
            <br />
            <br />
            <button onClick={(e) => this.handleUpload(e)} class="btn btn-red">Upload</button>
        </div>
    );
}
}
export default Design;  # <------sorry can't put this inside.

The images are successfully stored in images folder in django from react. but when i upload multiple files from react, django is receiving only one image.

please help me with some good solutions. any solution is appreciable. Thank you in advance.

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
Titan
  • 244
  • 1
  • 4
  • 18
  • 1
    `file = e.target.files[0]` in `handleFile` is only ever going to assign a single file to your state. Even if you did upload multiple files, I don't think your view/model would accept them – Iain Shelvington Jun 28 '20 at 19:27
  • to upload multples loop over all of them...not just the first and do `formdata.append('images[]', file[i])` – charlietfl Jun 28 '20 at 19:36
  • your right @Iain Shelvington if i remove index from files[0] to files, then i can see multiple files but django is not accepting. Is there any solutions? – Titan Jun 28 '20 at 20:09
  • @charlietfl even if i loop the django server not accepting mutiple files only accepting single file. thats the problem. what to do? – Titan Jun 28 '20 at 20:10
  • @EpicGamers this answer may help https://stackoverflow.com/a/40862505/548562 – Iain Shelvington Jun 28 '20 at 20:13
  • @IainShelvington Thank for your help, but still no clue, i will keep on try. after removing file[0] to file im getting image with 'id' in react console, like this: before removing file[0] : File{"album.jpg"} but after removing file[0] to file: 0: File{"album.jpg"}, may be am i supposed to use that id in django to work? sorry for inconvenience. – Titan Jun 28 '20 at 20:41
  • @IainShelvington https://stackoverflow.com/questions/62637036/upload-multiple-images-without-forms-in-django <----this is my actual question that i want to ask. – Titan Jun 29 '20 at 11:27

0 Answers0