| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1906 人关注过本帖
标题:如何实现表单提交和上传图片同时完成
只看楼主 加入收藏
海风308
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2008-10-28
结帖率:100%
收藏
 问题点数:0 回复次数:2 
如何实现表单提交和上传图片同时完成
如题,我开始想完成该动作,在一个form中,要上传图片,需要加上form 表单的属性:enctype="multipart/form-data",但是到action页面就得不到其他的文本参数的值。
要是去掉enctype="multipart/form-data"属性,就没办法完成上传图片动作,但是可以获取文本值。
我只好把两个动作分开写到两个表单中,但是我想制作成一起的,点击一个提交两个动作同时完成。请指教,谢谢。
搜索更多相关主题的帖子: 表单 
2008-11-07 14:22
海风308
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2008-10-28
收藏
得分:0 
注:我只在数据库中保存图片路径
2008-11-07 18:04
wyq03
Rank: 1
等 级:新手上路
威 望:2
帖 子:63
专家分:0
注 册:2005-9-14
收藏
得分:0 
jsp:
<form id="fileUploadForm" name="fileUploadForm" action="./wyqService.action"
        enctype="multipart/form-data" method="post">
    <input type="file" name="file" id="file" size="40"/><br>
    <input type="submit" name="uploadButton" id="uploadButton" value="开始上传"/>
    <input type="button" name="cancelUploadButton" id="cancelUploadButton" value="取消上传"/><br>
</form>
action:
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            // 上传在临时目录的临时文件
            FileInputStream fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);

            // 指定的文件存放地点
            FileOutputStream fos = new FileOutputStream(new File(dir,
                    getFileFileName()));
            bos = new BufferedOutputStream(fos);

            // 转移文件
            byte[] buf = new byte[4096];
            int len = -1;
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }

            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
            uploadary.add(getFileFileName() + "上传成功!");
        } catch (Exception e) {
            e.printStackTrace();
            uploadary.add(getFileFileName() + "上传失败!");
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

别拿无知当个性!
2008-11-07 18:15
快速回复:如何实现表单提交和上传图片同时完成
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020958 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved