Solved

upload file via FormData()

Posted on 2014-11-28
1
218 Views
Last Modified: 2014-12-11
Im trying to upload a file via an jQuery ajax request, so What I have is:-
HTML:-
<input type="file" id="uplLogo" />

Open in new window


jQuery:-
       $('#cmdSaveNewEntry').click(function () {
                var formData = new FormData();
                
                formData.append('function', 'Test');
                formData.append("userfile", $('#uplLogo')[0]files);
                
            
                $.ajax({
                    url: window.location.href + "function.php",
                    type: 'POST',
                    data: formData,
                    success: function (data)
                    {
                           alert(data);
                    }
                });
        });

Open in new window


But it doesnt seem to post the file Ive selected, just an empty request on the 'request.php'.

Ive done some googling and cannot find a decent workable solution.

Anyone have any ideas?

Thank you
0
Comment
Question by:tonelm54
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40470479
What you want to know (especially the file and jQuery parts) :
https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects#Sending_files_using_a_FormData_object

From yoru code, it look like you did not prevent to process the data with jQuery :

       $('#cmdSaveNewEntry').click(function () {
                var formData = new FormData();
                
                formData.append('function', 'Test');
                formData.append("userfile", $('#uplLogo')[0]files);
                
            
                $.ajax({
                    url: window.location.href + "function.php",
                    type: 'POST',
                    data: formData,
  processData: false,  // tell jQuery not to process the data
  contentType: false   // tell jQuery not to set contentType
                    success: function (data)
                    {
                           alert(data);
                    }
                });
        });

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

813 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

15 Experts available now in Live!

Get 1:1 Help Now