Solved

upload file via FormData()

Posted on 2014-11-28
1
215 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 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

12 Experts available now in Live!

Get 1:1 Help Now