0

So I have a upload form:

def upload(request):
    if request.method == 'POST':
        uploaded_file = request.FILES['file']
        fs = FileSystemStorage()
        fs.save(uploaded_file.name, uploaded_file)
    return render(request, 'nas/upload.html')

But I am not sure where to specify where the file gets uploaded to. I would like it to come from the currently active drive, which has an attribute Drive.path

Mehdi
  • 717
  • 1
  • 7
  • 21
Arthos
  • 443
  • 1
  • 5
  • 7

1 Answers1

0

We can do something like this:

def upload(request):

    if request.method == 'POST':

        # unpack request:
        uploaded_file = request.FILES['file']

        # save file locally:
        with open('/path/to/file/destination/file.extension', 'w+') as destination:
            destination.write(uploaded_file)

    return render(request, 'nas/upload.html')

More here: https://docs.djangoproject.com/en/3.0/topics/http/file-uploads/

Daniel
  • 3,228
  • 1
  • 7
  • 23
  • I tried this and changed the string to the wished location but I get an error: ```write() argument must be str, not InMemoryUploadedFile ``` – Arthos Feb 15 '20 at 16:08
  • Can you try changeing `'w+'` to `'wb+'` - what happens then? – Daniel Feb 15 '20 at 16:13
  • Here are some other solutions as well: https://stackoverflow.com/questions/3702465/how-to-copy-inmemoryuploadedfile-object-to-disk – Daniel Feb 15 '20 at 16:15
  • Well now I get ```a bytes-like object is required, not 'InMemoryUploadedFile' ``` – Arthos Feb 15 '20 at 16:20
  • I changed the path to 'C:/Users/User/Desktop/Python/RavNet/media/storage/drives/okay/file.extension'. Not sure if that file.extension needs to be at the end – Arthos Feb 15 '20 at 16:20