Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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 …

708 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

14 Experts available now in Live!

Get 1:1 Help Now