<s:file/>多文件上传

       <s:file/>有个属性multiple 在标签上添加这属性<s:file name="" id="" multiple/>这样就能同时选中多个文件来选择上传了

  upload.jsp

                 

                        <s:form action="resourceAction_addExercises" name="exerciseForm" id="exerciseForm" method="post" enctype="multipart/form-data">

                        <input type="file" name="exercise" multiple/>

                      </s:form>


后台Action

              

                     Resource resource[]=new Resource[20];//数组对象

                    private File[] exercise;//上传文件(与标签名一致)

                    private String[] exerciseFileName;//上传文件名
            private String[] exerciseContentType;//上传文件类型

                。。。。。。。。。。。。。分别实现get、set方法

               方法体

   

 try{
			int i;
			FileInputStream is=null;
			FileOutputStream fost=null;
			for(i = 0;i < exercise.length;i++){
				
					 is =  new FileInputStream(exercise[i]);
					
					//String root = ServletActionContext.getServletContext().getRealPath("/upload");//得到当前目录下的upload目录的绝对路径
					 fost = new FileOutputStream(ServletActionContext.getServletContext()
							   .getRealPath("exercises/" + exerciseFileName[i]));
						int length = 0;
						byte[] buffer = new byte[1024];
						while((length = is.read(buffer))!=-1){
							fost.write(buffer, 0, length);
						}
					ActionContext.getContext().getSession().put("exe", "文件上传成功!");
				
			is.close();
                       fost.close();
                                     }
                                        }catch(Exception e){
                                                 e.printStackTrace();
                                                 return INPUT;
                                              }

                                         return "toexercise";
   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请帮我修改实现视频上传服务器这是前端代码:<!DOCTYPE html> <!-- Coding By CodingNepal - youtube.com/codingnepal --> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>File Upload with Progress Bar | CodingNepal</title> <link rel="stylesheet" href="annotation/File Upload in JavaScript/style.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/> </head> <body> <div class="wrapper"> <header>File Uploader JavaScript</header> <form action="#"> <input class="file-input" type="file" name="file" hidden> <i class="fas fa-cloud-upload-alt"></i> <p>Browse File to Upload</p> </form> <section class="progress-area"></section> <section class="uploaded-area"></section> </div> <script src="annotation/File Upload in JavaScript/script.js"></script> </body> </html>,这是后端代码:<?php namespace app\index\controller; use think\Controller; use think\migration\db\Table; use Util\data\Sysdb; use app\index\controller\BaseAdmin; use think\Db; use think\Filesystem; // 在控制器文件的顶部添加这一句 use think\Request; class Test extends Controller { public function index(){ return $this->fetch(); } public function myMethod() { // 执行操作 // ... // 返回 AJAX 响应 $response = ['status' => 'success', 'message' => '操作成功']; return json($response); } public function upload() { return $this->fetch('test/upload'); } public function save(Request $request) { // 获取上传的文件 $file = $request->file('video'); // 将文件保存到服务器上 $saveName = Filesystem::disk('public')->putFile('videos', $file); // 将视频信息存储到数据库中 $data = [ 'filename' => $file->getOriginalName(), 'path' => $saveName, 'type' => 'video/' . $file->guessExtension(), 'size' => $file->getSize(), 'created_at' => date('Y-m-d H:i:s', time()) ]; Db::name('videos')->insert($data); // 返回上传结果 return json([ 'status' => 'success', 'filename' => $file->getOriginalName(), 'path' => $saveName ]); } }
最新发布
05-15

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值