Ajax File Upload

Ajax File Upload

You might have seen Ajax File Uploading in some sites. The basic idea is to upload a file without refreshing the page. Some sites go even further by providing details on how much percentage
is uploaded, etc. It is very easy to achieve this effect using javascript.

First off, I did not like using the term 'Ajax' when describing this method. But there is no other word that can be used. And this method fits within my definition of Ajax - "Sending or receiving data
from the server without refreshing the page.". So I will name this method Ajax file upload.

Theory

The basic idea behind this effect is to redirect a form's action into a hidden IFrame. So, when the form with the file field is submitted, the result will appear in a IFrame. But since it is hidden,
the user will not see it. This effect is achieved using the 'target' attribute of the Form tag.

See the small squire? That is the IFrame. Typically, it will be hidden.

Code

(X)HTML

<form id="file_upload_form" method="post" enctype="multipart/form-data" action="upload.php">
<input name="file" id="file" size="27" type="file" /><br />
<input type="submit" name="action" value="Upload" /><br />
<iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>

JavaScript

function init() {
	document.getElementById('file_upload_form').onsubmit=function() {
		document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe
	}
}
window.onload=init;

PHP

This should be in upload.php - the action of the form.

<?php
include('../../../common.php');
upload('file','.','txt,jpg,jpeg,gif,png');

The code to upload the file must be given here. I used one of my custom functions to upload
the file
 - it is not a native PHP function. Needless to say, this is not restricted to PHP - you can use any server side language here.

Update: Part two - Ajax File Upload Response Handling

//Some funny comments:

时间: 2024-10-08 20:03:27

Ajax File Upload的相关文章

Ajax File Upload Response Handling

Ajax File Upload Response Handling A while ago, I wrote an article on Ajax File Uploading - the method of uploading a file without refreshing the page using a hidden iframe. Since then people wanted to know how to get information about the uploaded f

jQuery Ajax File Upload实例源码_jquery

本文实例为大家分享了jQuery Ajax File Upload实例源码,供大家参考,具体内容如下 项目结构 Default.aspx Upload.aspx Scripts/- style.css 效果图   客户端html代码 <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="UploadFile.aspx.vb" Inherits="Web.UploadF

不用common file upload、struts 什么的,java文件上传功能怎么实现?

问题描述 不用common file upload.struts 什么的,java文件上传功能怎么实现? 不用common file upload.struts 什么的,java文件上传功能怎么实现,就单纯的用html和java实现文件上传? 解决方案 http://wangwei2008224078.blog.163.com/blog/static/214338275201342110555388/ 这个什么也没用,你可以参考 解决方案二: 用servlet的request获取文件流 解决方案

jQuery File Upload文件上传插件使用详解_jquery

jQuery File Upload 是一个Jquery文件上传组件,支持多文件上传.取消.删除,上传前缩略图预览.列表显示图片大小,支持上传进度条显示:支持各种动态语言开发的服务器端. 官网链接:https://github.com/blueimp/jQuery-File-Upload/wiki 特点:拖放支持:上传进度条:图像预览:可定制和可扩展的:兼容任何服务器端应用平台(PHP, Python, Ruby on Rails, Java, Node.js, Go etc.). 使用方法:

IE 8中jquery file upload 上传图片没反应

问题描述 IE 8中jquery file upload 上传图片没反应 jquery file upload 组件在chrome中好使 在IE中愣是没反应,前台的请求发送不到后台Controller中 后台SpringMVC 接受请求如下 @RequestMapping(value = "/fileupload", method = RequestMethod.POST) public @ResponseBody HBaseFileInfo upload(HttpServletReq

用jQuery File Upload实现简单的文件上传

FORM中的代码: {# file_path #} <div class="form-group"> <label class="control-label col-md-3"> {{ form.file_path.label_tag }} {% for error in form.file_path.errors %} <span class="label label-danger">{{ error }}&

PHP AJAX无刷新上传图片实例

将index.html和upload.php文件保存到apache工作目录,例子使用安装目录D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/ AJAX 客户端页面代码: index.html <html> <body> <h1>Ajax file upload sample</h1><br/><input id="uplaod" name=&quo

php ajax无刷新上传图片实例代码

将index.html和upload.php文件保存到支持php的空间的同级目录,测试运行即可 AJAX 客户端页面代码: index.html   <html> <body> <h1>Ajax file upload sample</h1><br/><input id="uplaod" name="btn_send" type="button" value="上传测试&

5款Ajax 文件上传控件_AJAX相关

1. FancyUpload (演示地址) FancyUpload是一个采用Flash与Ajax(MooTools)技术实现包含上传进度条的多文件上传组件,类似于SWFUpload. 2. SwfUploadPanel (演示地址) SwfUploadPanel是一个结合SwfUpload v2.0.2与ExtJS 2.0.x开发的多文件上传面板(panel). 3. Yahoo! UI Library: Uploader (演示地址) YUI Uploader Control结合Flash开发