Solved

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

Posted on 2011-03-06
2
539 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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?
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…
The viewer will learn how to count occurrences of each item in an array.

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