jquery ajax form and file upload.

Posted on 2010-01-04
Last Modified: 2012-05-08
I've been using jquery.ajax to post form data out of a dialog and its been working great except now I'm trying to add a file field to the form and I'm not sure how to send multipart/form-data.

the code I was using to upload the form is below.  userfile is the filefield.

// JavaScript Document
var title; 
 var content;
 var windowID; 
  var contentType;
 var formType; 
 var userfile;

function contentFormAjaxStuff(){
title = jQuery("#title").attr("value");
content = jQuery("#content").attr("value");
windowID = jQuery("#windowID").attr("value");
formType = jQuery("#formType").attr("value");
contentType = jQuery("#contentType").attr("value");
 userfile = jQuery("#userfile").attr("value");

  type: "POST",
  url: "backendContent.php",
  cache: false,

  dataType: "html",
  data: "title=" + title + "&content=" + content + "&windowID=" + windowID + "&contentType=" + contentType + "&formType=" + formType + "&userfile=" + userfile,
  success: function(response){
   // if sucessful; response will contain some stuff echo-ed from .php
   alert("Here is your response: " + response);
   // Append this response to some <div> => let's append it to div with id "serverMsg"

  error: function(){
   alert("Error occured during Ajax request...");
} // END OF FormAjaxStuff()

Open in new window

Question by:jimfrith
    LVL 40

    Expert Comment

    LVL 2

    Accepted Solution

    enctype is not supported by jquery ajax thus you cant upload files using it as you do...

    check here

    esp the upload jquery plugin... they do it well

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now