[ACCEPTED]-Django upload file into specific directory that depends on the POST URI-filenames

Accepted answer
Score: 11

Fixed olooney example. It is working now

@csrf_exempt
def upload_video_file(request):
    folder = 'tmp_dir2/' #request.path.replace("/", "_")
    uploaded_filename = request.FILES['file'].name
    BASE_PATH = '/home/'
    # create the folder if it doesn't exist.
    try:
        os.mkdir(os.path.join(BASE_PATH, folder))
    except:
        pass

    # save the uploaded file inside that folder.
    full_filename = os.path.join(BASE_PATH, folder, uploaded_filename)
    fout = open(full_filename, 'wb+')

    file_content = ContentFile( request.FILES['file'].read() )

    try:
        # Iterate through the chunks.
        for chunk in file_content.chunks():
            fout.write(chunk)
        fout.close()
        html = "<html><body>SAVED</body></html>"
        return HttpResponse(html)
    except:
        html = "<html><body>NOT SAVED</body></html>"
        return HttpResponse(html)

0

Score: 5

Django gives you total control over where 35 (and if) you save files. See: http://docs.djangoproject.com/en/dev/topics/http/file-uploads/

The below 34 example shows how to combine the URL and 33 the name of the uploaded file and write 32 the file out to disk:

def upload(request):
    folder = request.path.replace("/", "_")
    uploaded_filename = request.FILES['file'].name

    # create the folder if it doesn't exist.
    try:
        os.mkdir(os.path.join(BASE_PATH, folder))
    except:
        pass

    # save the uploaded file inside that folder.
    full_filename = os.path.join(BASE_PATH, folder, uploaded_filename)
    fout = open(full_filename, 'wb+')
    # Iterate through the chunks.
    for chunk in fout.chunks():
        fout.write(chunk)
    fout.close()

Edit: How to do this 31 with a FileUploadHandler? It traced down 30 through the code and it seems like you need 29 to do four things to repurpose the TemporaryFileUploadHandler 28 to save outside of FILE_UPLOAD_TEMP_DIR:

  1. extend 27 TemporaryUploadedFile and override init() to pass through a different 26 directory to NamedTemporaryFile. It can 25 use the try mkdir except for pass I showed 24 above.

  2. extend TemporaryFileUploadHandler and override new_file() to 23 use the above class.

  3. also extend init() to accept 22 the directory where you want the folder 21 to go.

  4. Dynamically add the request handler, passing 20 through a directory determined from the 19 URL:

    request.upload_handlers = [ProgressBarUploadHandler(request.path.replace('/', '_')]

While 18 non-trivial, it's still easier than writing 17 a handler from scratch: In particular, you 16 won't have to write a single line of error-prone 15 buffered reading. Steps 3 and 4 are necessary 14 because FileUploadHandlers are not passed 13 request information by default, I believe, so 12 you'll have to tell it separately if you 11 want to use the URL somehow.

I can't really 10 recommend writing a custom FileUploadHandler 9 for this. It's really mixing layers of 8 responsibility. Relative to the speed of 7 uploading a file over the internet, doing 6 a local file copy is insignificant. And 5 if the file's small, Django will just keep 4 it in memory without writing it out to a 3 temp file. I have a bad feeling that you'll 2 get all this working and find you can't 1 even measure the performance difference.

More Related questions