Solved

jquery $.ajax processing the value in type="file"

Posted on 2011-03-06
2
529 Views
Last Modified: 2012-08-14
Hi guys,

How do you add the value of what is sitting in a html form type of 'file' to be processed when in a jquery ajax function?  

i.e. this is my script so far:

$.ajax({
  type: "POST",
  url: "resultspage.php",
  data:  $("#form").serialize()+"&pic="+document.form1.pic.value,
  cache: false,
  dataType: "html",
  success: function(responseText){
    $("#displaydiv").html(responseText);
  }
});

When i run this, i get the value of:  "C:\fakepath\filename.jpg"  from the pic field.
And I can't find a way for serialize() to pass this value in either.

What is best used to capture this value so that it is just the file name?

Many Thanks
Paul



0
Comment
Question by:dev09
  • 2
2 Comments
 
LVL 15

Accepted Solution

by:
ncoo earned 500 total points
ID: 35045987
You could use regex like so to get the name:
path = document.form1.pic.value;

filename = path.replace(/^.*\\/, '');

 data:  $("#form").serialize()+"&pic="+filename,

Open in new window

0
 
LVL 15

Expert Comment

by:ncoo
ID: 35045998
If you're talking about actually uploading the file and not just getting the filename, take a look at: http://valums.com/ajax-upload/
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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)

831 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