Solved

upload file via FormData()

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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.

707 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