Solved

upload file via FormData()

Posted on 2014-11-28
1
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

730 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