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

  • Home
  • SEARCH
  • 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 7950433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:16:17+00:00 2026-06-04T02:16:17+00:00

I created a no javascript code which uses an html form and django view

  • 0

I created a no javascript code which uses an html form and django view to upload a file .It uses an html5 input element for selecting the file.On clicking the submit button,the file gets uploaded without any problem.The django view can get the file from request.FILES

def upload_file(request,template_name):
    to_return = {}
    store_message="failure"
    if request.method == 'POST':
        if request.FILES.has_key('fselect'):
            file = request.FILES['fselect']
            with open('/uploadpath/%s' % file.name, 'wb+') as dest:
                for chunk in file.chunks():
                    dest.write(chunk)
            store_message="success"
    to_return['store_message']= store_message
    if store_message == "failure":
        return redirect('home')
    reqctx = RequestContext(request,to_return)
    return return render_to_response(template_name,reqctx)

the html form is

    <form enctype="multipart/form-data" method="post" action="{% url uploaded %}"> {% csrf_token %}
    <input type="file" name="fselect" id="fselect">    </input>
    <input type="submit" id="uploadbtn" value="upload">    
    </form>

Now I want to use some javascript to call the django view .
I have coded ajax version of upload view

def ajax_upload_file(request):
    to_return = {}
    store_message="failure"
    if request.method == 'POST':
        if request.FILES.has_key('fselect'):
            file = request.FILES['fselect']
            with open('/uploadpath/%s' % file.name, 'wb+') as dest:
                for chunk in file.chunks():
                    dest.write(chunk)
            store_message="success"
     to_return['store_message']= store_message
     serialized = simplejson.dumps(to_return)
     if store_message == "failure":
        return HttpResponseServerError(serialized, mimetype="application/json")
     else:
        return HttpResponse(serialized, mimetype="application/json")

I am confused about how the file object can be passed from javascript code to django view.

javascript code

$(document).ready(function(){
  $('#fselect').change(function(){uploadFile()});

}
function uploadSubTitleFile(){
    //check if it is a subtitle file?
    var file=document.getElementById('fselect').files[0];
    var data = {  };//how to pass the data
    var args = { type:"POST", url:"ajax_upload/", data:data, complete:done };    
    return;
}
  • 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-04T02:16:19+00:00Added an answer on June 4, 2026 at 2:16 am

    For the simple answer, you don’t. Send your form into an hidden iframe and and let the javascript call its parent window.

    Since file uploading occurs through an iframe, traditional response data such as HTTP status codes are not directly available, and connection manager cannot reasonably discern success or failure (except a transaction timeout). Instead, the callback’s upload handler will receive a response object containing the body of the iframe document, as a string, when the transaction is complete.

    — http://developer.yahoo.com/yui/connection/#upload

    My advise is, find a decent library that does that for you and use it:

    • https://github.com/GoodCloud/django-ajax-uploader
    • http://www.uploadify.com/
    • …

    With recent browsers, it’s possible to do it without relying on an iframe: Sending multipart/formdata with jQuery.ajax

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I created a JavaScript file called test.js which contains the following code:
I have a dialog box, created with the code javascript and HTML generated below.
I have created an xPages application which uses a lot of server side javascript
I created a javascript application with all of the code in one file. The
I'm getting a function not defined Javascript error with the following code, which uses
the javascript:alert code that is being dynamically created ONLY works if I use the
I am trying to create a new Element in my javascript code and append
I have developed a Firefox toolbar in XUL, which uses javascript to manipulate the
I have created an applet which has a necessity to execute the following code:
I have some Javascript code that needs to be dynamically created as a String

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.