0

Upload a file, but not inside a form

For example :

<input type="file" name="taskuploadfile"  />

<input type="button" name="taskupload" value="Task Upload" onclick="taskupload()" />

Using Ajax for javascript to php.

Can I get temp path of file in php or Is it possible ?

Justin John
  • 9,223
  • 14
  • 70
  • 129

4 Answers4

1

You can use the File API to read the data and then send it via XHR as per an example on MDN or by using XHR2 as per HTML 5 Rocks' example.

These methods do have limited browser support though, so you are probably still better off using a real form and submitting to an iframe for the time being.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Use a library that can do it the best way that you can not reach even after a month of coding.

One of the best examples is JqUploader.

Here is the example: http://pixeline.be/experiments/jqUploader/test.php

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
0

Since it looks like you are using dojo (from the tags you put in your post)... why not using the HTML5 multi-file upload widget ? It has a plugin to do ajax uploads...

More on that here : http://dojotoolkit.org/documentation/tutorials/1.6/uploader/

Reference documentation here : http://dojotoolkit.org/reference-guide/dojox/form/Uploader.html

Philippe
  • 6,703
  • 3
  • 30
  • 50
-1

It is not possible to do a file upload with ajax. The only trick is to use an IFRAME. And since you want to POST something to the server, you have to use a form tag.

Freeman
  • 1,201
  • 1
  • 11
  • 20
  • 1
    It is possible to do file uploading with ajax, without the iframe. – zatatatata Jan 05 '12 at 13:16
  • 2
    http://stackoverflow.com/questions/543926/is-it-possible-to-use-ajax-to-do-file-upload – Freeman Jan 05 '12 at 13:27
  • @Vahur Roosimaa, I did ajax based file upload, but I used hidden iframe. I really appreciate if you can guide me on "Doing upload with AJAX without iframe. Any tutorial. When I search this, I only found IFRAME + Ajax technique. Thank you so much. – Prasad Rajapaksha Jan 05 '12 at 13:30
  • @PrasadRajapaksha It can be quite difficult if you're not experience with how HTTP requests work since you must build a request by hand. Some browsers allow you to do it without building headers manually, but for best backward compatibility you should use a library such as this http://github.com/valums/file-uploader I have used it myself and works really well, and falls back to iframe based uploading if the browser doesn't support more modern ways. – zatatatata Jan 05 '12 at 13:56
  • @FreemanLatif the answer you posted is from over 2 years ago. In the meantime HTML5 has arrived. – zatatatata Jan 05 '12 at 13:58
  • He is right - it isn't possible in in all browsers.. at least you need a fallback using iframes ;) – Walialu Jan 05 '12 at 15:03
  • XmlHttpRequest does not traditionally support file uploads. Although some of the newest browsers have changes to the specification, most browsers in the wild still do not support it. Using iframes guarantees compatibility across all browsers, so for me it's still the best solution. – Freeman Jan 06 '12 at 13:59
  • @Walialu so we should all rely on pure text to support every single browser that doesn't yet (traditionally they didn't) render HTML and ride around on horses because there aren't roadways to everywhere? No. You should use the latest technologies to offer the best possible experience for the user, and offer a fallback solution for those who don't yet use up to date software. Suggesting using something more modern also gives them incentive to switch to more secure platforms, resulting in happier customers and developers. The new APIs are long ready to be used, and everybody should embrace them. – zatatatata Jan 09 '12 at 11:33
  • @Vahur Roosimaa just read over what i wrote ;) "at least you need a fallback using iframes" – Walialu Jan 09 '12 at 12:56
  • Slippery slope there. It all depends on what you need. Sites like facebook are built to work on IE6 while if you want the latest cool stuffs like CSS3 animations you do need to sacrifice those cave-dwellers who're still using defunct browsers. Of course it's not wrong to use the latest API but it all depends on which market are you targeting. I don't expect my grandpa to ever update his browser but he does buy stuffs online. Never underestimate backward-compatibility. – Freeman Jan 09 '12 at 14:42
  • From what sentence did you read that I suggest not doing backward compatible sites (or underestimate it)?? If you don't know how to use the latest APIs for best performance and have fallbacks, then you should change businesses. It's not a slippery slope if you know what you are doing. The fact that you have to have fallback doesn't have ANYTHING to do with using the latest APIs. Also I can bet that you've never had (and never will) have a project that specifically needs IE6 to be perfectly supported as it's impossible. It's like showing a black and white picture on your 50" brand new HD TV. – zatatatata Jan 09 '12 at 20:24