Solved

upload file via FormData()

Posted on 2014-11-28
1
211 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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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 …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

16 Experts available now in Live!

Get 1:1 Help Now