工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 2990|回复: 4

ajax上传文件

[复制链接]
发表于 2007-4-6 14:02 | 显示全部楼层 |阅读模式
今天想到了ajax上传文件的问题,不知怎样解决;于是上网搜索,在网上看到了好多文章,看到有人使用Prototype上传其实使用还是基本的方式进行
的,没有使用ajax提交文件,实现方式是使用form方式提交服务器端,然后使用ajax方式到服务器端查询服务器端文件上传的进度,然后
在客户端的进度条上显示进度,没有从更本上使用ajax方式进行上传;最终了解到其实要使用ajax上传文件要在客户端先把文件
使用javascript方式读取出来然后将读取内容通过send的方式提交到服务器端,使用这种方式可能对于客户端来说不太安全,因为脚本需要有
访问客户文件系统的权限;使用ajax方式上传文件找到了2种做法(在老外的网站上找的)一种使用的是在firefox浏览器下实现的,通过
修改浏览器设置可以访问本地文件系统,然后调用浏览中一个控件方式读取文件的内容,将文件内容和文件名称等信息组合成http协议上传文件的形势
然后通过ajax的open方法和send方法提交到服务,我觉得这种方法有些局限性(现在毕竟使用firefox的用户还是不太多),不过在firefox下能够显示已经不错了;
 楼主| 发表于 2007-4-6 14:05 | 显示全部楼层
SPRING 中也有支持文件上传的接口,还有jspsmart,commons-fileupload。。。。
Spring支持的文件上传的接口和实现都在org.springframework.web.multipartorg.springframework.web.multipart.commonsorg.springframework.web.multipart.cosorg.springframework.web.multipart.support
几个包里边。分别来看看几个包到底有什么。
1.org.springframework.web.multipart包括有MultipartFileMultipartHttpServletRequestMultipartResolver三个接口,很明显multipart提供了最基本的文件上传的接口。每个接口各有什么方法呢?
1.1.MultipartFile
看api说明
public interface MultipartFile
A representation of an uploaded file received in a multipart request.
The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storages will be cleared at the end of request processing.
方法有:
getBytes()
          Return the contents of the file as an array of bytes.
getContentType()
          Return the content type of the file.
getInputStream()
          Return an InputStream to read the contents of the file from.
getName()
          Return the name of the parameter in the multipart form.
getOriginalFilename()
          Return the original filename in the client's filesystem.
getSize()
          Return the size of the file in bytes.
isEmpty()
          Return whether the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.
transferTo(File dest)
          Transfer the received file to the given destination file.
1.2.MultipartHttpServletRequest 接口,实现类有AbstractMultipartHttpServletRequest, CosMultipartHttpServletRequest, DefaultMultipartHttpServletRequest, MockMultipartHttpServletRequest 四个。
public interface MultipartHttpServletRequest
extends HttpServletRequest
Provides additional methods for dealing with multipart content within a servlet request, allowing to access uploaded files. Implementations also need to override the standard ServletRequest methods for parameter access, making multipart parameters available.
看到名字和看到它继承了 HttpServletRequest,大家应该大概猜到它的作用了吧。
具体的方法有:
getFile(String name)
          Return the contents plus description of an uploaded file in this request, or null if it does not exist.
getFileMap()
          Return a Map of the multipart files contained in this request
getFileNames()
          Return an Iterator of String objects containing the parameter names of the multipart files contained in this request.

1.3.MultipartResolver 接口,实现类有CommonsMultipartResolver, CosMultipartResolver 两个

[ 本帖最后由 hexq 于 2007-4-6 14:32 编辑 ]
回复

使用道具 举报

发表于 2007-4-6 14:31 | 显示全部楼层
即时回送上传进度,不知PHP可不可以做到...
回复

使用道具 举报

 楼主| 发表于 2007-4-6 14:33 | 显示全部楼层
PHP没有试过。。。
回复

使用道具 举报

 楼主| 发表于 2007-4-6 14:44 | 显示全部楼层
发觉我还是没有做到即时回送上传进度。。。。。还只是实现使用form方式提交服务器端,然后使用ajax方式到服务器端查询服务器端文件上传的进度,然后
在客户端的进度条上显示进度,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-16 00:11

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表