1.html部分

<input style="width: 280px" type="file" name="upLoadProjectPlan" id="upLoadProjectPlan"
     value="<%=taskAppend.getTaskAllocationDoc()%>"/> <a style="float: right; margin-right: 40px" class="button" href="javascript:void(0)"
onclick="UpladFile('upLoadProjectPlan', '<%=task.getTaskid() %>')"><span>上传</span></a>

2.js中利用ajax产生表单并发送表单

function UpladFile(fileUploadId, taskid) {

         var fileObj = document.getElementById(fileUploadId).files[0]; // 获取文件对象

         var FileController = "updateWorkAction!fileUpload"; // 接收上传文件的后台处理程序地址 

         // FormData 对象

         var form = new FormData();

         //form.append("author", "hooyes");                        // 可以增加表单数据
form.append("taskid", taskid);
form.append("file", fileObj); // 文件对象 // XMLHttpRequest 对象 var xhr = new XMLHttpRequest(); xhr.open("post", FileController, true); xhr.onload = function () {
location.reload(true);
alert("上传完成!"); }; xhr.send(form);
}

3.Action部分

public class UpdateWorkAction extends ActionSupport{
private DistributeDao distributeDao;
private String taskid;
private List<File> file; // 上传的文件
private List<String> fileFileName; // 文件名称
private List<String> fileContentType; // 文件类型 public DistributeDao getDistributeDao() {
return distributeDao;
} public void setDistributeDao(DistributeDao distributeDao) {
this.distributeDao = distributeDao;
} public List<File> getFile() {
return file;
} public void setFile(List<File> file) {
this.file = file;
} public List<String> getFileFileName() {
return fileFileName;
} public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
} public List<String> getFileContentType() {
return fileContentType;
} public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
} private void init() throws UnsupportedEncodingException{
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
ServletContext context = ServletActionContext.getServletContext();
} public String fileUpload() throws Exception{
init();
// 取得需要上传的文件数组
List<File> files = getFile();
if (files != null && files.size() > 0) {
for (int i = 0; i < files.size(); i++) {
FileOutputStream fos = new FileOutputStream(getSavePath() +
                           "\\" + getFileFileName().get(i));
FileInputStream fis = new FileInputStream(files.get(i));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
TaskAppend taskAppend = distributeDao.findLeaderMsg(Integer.parseInt(taskid));
taskAppend.setTaskAllocationDoc(getFileFileName().get(0));
distributeDao.updateTaskAppend(taskAppend);
}
return "fileUpload";
} public String getTaskid() {
return taskid;
} public void setTaskid(String taskid) {
this.taskid = taskid;
}
}

ajax实现上传文件的更多相关文章

  1. Ajax方式上传文件

    用到两个对象 第一个对象:FormData 第二个对象:XMLHttpRequest 目前新版的Firefox 与 Chrome 等支持HTML5的浏览器完美的支持这两个对象,但IE9尚未支持 For ...

  2. koa2:通过Ajax方式上传文件,使用FormData进行Ajax请求

    koa2通过表单上传的网上很多,但通过Ajax方式上传文件,使用FormData进行Ajax请求,不好找. 参考了这个用base64上传图片的例子.https://github.com/Yuki-Mi ...

  3. (23)ajax实现上传文件的功能

    form表单上传文件 urls.py from django.conf.urls import urlfrom django.contrib import adminfrom app01 import ...

  4. SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)

    1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...

  5. ajax异步上传文件和表单同步上传文件 的区别

    1. 用表单上传文件(以照片为例)-同步上传 html部分代码:这里请求地址index.php <!DOCTYPE html> <html lang="en"&g ...

  6. ajax如何上传文件(整理)

    ajax如何上传文件(整理) 一.总结 一句话总结:用FormData,FormData+ajax=异步上传二进制文件 <form enctype="multipart/form-da ...

  7. django中通过文件和Ajax来上传文件

    一.通过form表单来上传文件 1.在html模板中 <form action="/index/" method="post" enctype=" ...

  8. 基于Flask开发网站 -- 前端Ajax异步上传文件到后台

    大家好,我是辰哥~ 辰哥最近利用空闲时间在写一个在线可视化平台,过程中也觉得一些技术还是比较有意思的,所以就以模块化的形式分享出来.如:从网页界面(前端)上传文件到服务器(后端). 放一下该模块的界面 ...

  9. 通过Ajax方式上传文件,使用FormData进行Ajax请求

    通过传统的form表单提交的方式上传文件: <form id= "uploadForm" action= "http://localhost:8080/cfJAX_ ...

随机推荐

  1. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  2. linux platform设备与驱动

    struct platform_driver { int (*probe)(struct platform_device *); int (*remove)(struct platform_devic ...

  3. Android与H5交互

    1.初始化WebView控件 webView = (WebView) findViewById(R.id.webview); 2.设置WebView属性 WebSettings webSettings ...

  4. Torch7学习笔记(四)StochasticGradient

    使用随机梯度下降训练神经网络 StochasticGradient是一个比较高层次的类,它接受两个参数,module和criterion,前者是模型结构,后者是损失函数的类型.这个类本身有一些参数: ...

  5. PHP的数组排序函数

    <?php class order{ /** * * 数组排序 * @param array $arr 例如: * array ( array ( 'deskId' => '460646' ...

  6. 向mysql中插入Date类型的数据

    先看数据库表的定义 date字段为sql.date类型.我要向其中插入指定的日期和当前日期. 一.插入当前日期 思路:先获取当前系统,在将当前系统时间转换成sql类型的时间,然后插入数据库.代码如下 ...

  7. jquery mobile 问问多多

    jquery mobile  问题多多,兼容性太差.android4.1下完全崩溃.以后再也不用jquery mobile了

  8. Excel表格数据导入到SQLServer数据库

    转载:http://blog.csdn.net/lishuangzhe7047/article/details/8797416 步骤: 1,选择要插入的数据库--右键--任务--导入数据 2,点击下一 ...

  9. ql 判断 函数 存储过程是否存在的方法

    下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助. 库是否存在 if exists(select * from master..s ...

  10. Zend Studio XDebug调试配置

    最近在配置zend studio时找了些资料,发现了这个,说的比较详细 搭建Zend Studio 10.5 和XDebug 环境,试图进行 Drupal的调试, 经历了一些困难,但是最终解决了问题, ...