Upload file via ajax using php

I would like to know if it is possible to download a binary via ajax and php and have a link to download it. I would like to avoid updating the whole page, as with the standard html form. So far, I have used forms to retrieve information, such as radio and text fields, and use javascript to override the default behavior. Is it possible to download a file like this?

+4
source share
8 answers

Unable to send file through Javascript.

Your options:

  • A hidden iframe trick popularized by Google. Introducing this can lead to some klunky stuff, so there are libraries like jQuery that have plugins like jQuery, a popular form plugin that automates this, so you don't need to feel dirty while using it.
  • Using Flash to simplify the process. SWFUpload is especially noticeable. Other things being equal, I would probably use the Javascript solution, but I have used this in the past with success. The best part about this solution is that it has a more convenient interface, such as download indicators and thumbnails, etc. At the moment, however, you are asking the user to have Flash + Javascript, which may not work in some situations.
  • Using Silverlight instead of Flash, although I would not consider this a viable solution, since it has a much lower penetration rate than the other two solutions.
+13
source

While you cannot upload the file through AJAX, you can place the control file in a popup window and then refresh the page that spawned the popup window when it closes.

I don’t understand how to refresh the page that generated the popup, but I saw this in the ANGEL Learning Management Suite.

+1
source

Have an IFrame (display: none) in your form and set an iframe for your purpose.

My form is as follows:

<iframe id="upload_target" name="upload_target" src="" style="width:0px;height:0px;border:0px solid #fff;"></iframe> <form enctype="multipart/form-data" name="frmXMLUpload" target="upload_target" action="scripts/uploadXML.php" method="POST" onSubmit="return checkExtension(fXMLFile.value, 'xml')"> <!--only allow up to 30k of data to be uploaded--> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <input name="fXMLFile" id="fXMLFile" type="file" accept="text/xml" size="50" /> <p><input type="submit" value="Upload" /></p> </form> 

And answer the answer in IFRAME. basically it is not AJAX at all, but who would like JavaScript to have access to files on your local computer? It is beautiful as it is.

+1
source

As a rule, this cannot be done only in AJAX / Javascript.

Take a look at:

http://www.codeplex.com/SLFileUpload/

in order to do this in the silverlight application.

or

http://www.element-it.com/Examples/MultiPowUpload/SimpleUpload.html

in flash memory.

0
source

As John Gitzen said, you cannot do this directly through AJAX (i.e. send actual data via AJAX). What could you do if you created an invisible iframe form and then use AJAX to ask the server for the URL. This will preserve the basic user interface - without refreshing the page.

0
source

Have you considered jquery? There are beautiful plugins for this.

Like this, for example: http://www.phpletter.com/Our-Projects/AjaxFileUpload/

0
source

Take a look at this example, I'm a little better plugin than the one in phpLetter, although phpLetter may have better examples for PHP stuff.

http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Examples

0
source

You can also see SwfUploader , which downloads files using Flash, showing a progress bar, and without the need for a page to update. Demos can be seen here .

0
source

All Articles