Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:18:04+00:00 2026-06-09T12:18:04+00:00

I have the HTML form which do prior upload of files via AJAX. So

  • 0

I have the HTML form which do prior upload of files via AJAX. So in django backend I have the following code of View, which process this AJAX calls:

@csrf_exempt
def book_upload(request):
    if request.method == 'POST':
        log.info('received POST to main book_upload view')
        if request.FILES is None:
            return HttpResponseBadRequest('Must have files attached!')

        log.info('request has FILES')

        file_types = (u'file_pdf', u'file_djvu', u'file_doc', u'file_epub', u'file_djvu', u'file_fb2', u'file_txt', u'file_chm', u'file_other');
        file = None
        file_type = None
        for ft in file_types:
            if ft in request.FILES:
                file = request.FILES[ft]
                file_type = ft
                break

        if file is None:
            return HttpResponseBadRequest('Bad file type')

        file_path = file.temporary_file_path()

        result = {"path": file_path, "format": file_type}
        response_data = simplejson.dumps(result)

        if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
            mimetype = 'application/json'
        else:
            mimetype = 'text/plain'
        return HttpResponse(response_data, mimetype=mimetype)
    else:
        return HttpResponse('Only POST accepted')

But there is the problem in this code. It works on files more than 2.5 mb (because of TemporaryUploadedFile used than file size is > 2.5 mb by default settings). So and this code is based on idea that request.FILES contains object with TemporaryUploadedFile type. But in some cases I receive files with size < 2.5 mb. And request.FILES contains InMemoryUploadedFile.

So, I want do the following – each file, which is uploaded via ajax should be temporary stored. And memory is not a good place do that – because final re-storing files (after form submit) will not have information about this file in memory. So, the task is to “convert” InMemoryUploadedFile to TemporaryUploadedFile – is it possible?

PS

Maybe I should simply read file content from InMemoryUploadedFile object and write it to disk manually (to /tmp directory for example). How do you think?

PPS

And one another question – is it a good idea to do prior upload to temp directory?:) In my case the form have 6 input[type=file] elements, each of them upload different file type (six is for UI needs).

TIA!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:18:05+00:00Added an answer on June 9, 2026 at 12:18 pm

    You can override the default in FILE_UPLOAD_HANDLERS in settings.py

    # only use TemporaryFileUploadHandler for file uploads
    FILE_UPLOAD_HANDLERS = (
        'django.core.files.uploadhandler.TemporaryFileUploadHandler',
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex HTML form which I want to send via email. Is
i have an html form ,php scrip and jquery. i need a ajax code
I have an HTML form which submits values to the following PHP file, which
I have an HTML form which, when submitted by the user, will call a
I have an html form which when it gets submitted it calls a JavaScript
I have a html form which have a select list box from which you
I have this html form which has options: <tr> <td width=30 height=35><font size=3>*List:</td> <td
I have a HTML form which allows a user to add rows ad hoc
I have a simple html login form which is validated with javascript before its
I have an HTML Contact us form which I plant on my client's HTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.