fastapi+layui多文件上传

带进度条的多文件上传

--------------------------------------------- 
python
-------------
from fastapi import FastAPI,Header,File,UploadFile
app = FastAPI()
# 允许跨域配置
origins = [
    "http://127.0.0.1",
    "http://localhost:8080",
    "http://lj.hk1.myxinhai.cn",
    "http://ljweb.hk1.myxinhai.cn:8080",
    "http://dz.hk1.myxinhai.cn:8080"
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


from typing import List
@app.post("/file")
async def read_item(file: List[UploadFile] = File(...)):
    for files in file:
        t = await files.read()
        with open(files.filename,"wb") as f:
            f.write(t)
        print(files.filename)

    return {"code": 0,"msg": "","files": {"file": "ok"}}

-----------------------------------------
html
---------------
1.修改modules/upload.js文件
在 dataType:"json" 后面加入

xhr:function () {
	var newXhr = i.ajaxSettings.xhr();
	// 给xhr的upload添加progress的监听
	newXhr.upload.addEventListener('progress' , function (e) {
		var percent = Math.floor(e.loaded / e.total * 100); //计算出进度
		typeof l.progress === 'function' && l.progress(e , percent); // 传递给upload的progress回调
	});
	return newXhr;
},



<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>文件管理</cite></a>
  
  </div>
</div>
	  
		  
<div class="layui-row">
    <div class="layui-col-md1">
      <div class="grid-demo grid-demo-bg1">&nbsp;</div>
    </div>
    <div class="layui-col-md8">
      <div class="grid-demo">
	     <div class="layui-card">
        <div class="layui-card-header">
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
		<legend></legend>
		</fieldset> 
		</div>
        <div class="layui-card-body">
         
		 
		  
<div class="layui-upload">
  <button type="button" class="layui-btn layui-btn-normal" id="testList">选择文件</button> 
    <button type="button" class="layui-btn" id="testListAction">开始上传</button>

  <div class="layui-upload-list">
    <table class="layui-table">
      <thead>
        <tr><th>文件名</th>
        <th>进度</th>
        <th>状态</th>
        <th>操作</th>
      </tr></thead>
      <tbody id="demoList"></tbody>
    </table>
  </div>
</div>
		 
		 
		 
		 
		 
        </div>
      </div>
	  


	  
	  
	  
	  
	  
	  
	  
	  
	  
	  </div>
    </div>
    <div class="layui-col-md1">
      <div class="grid-demo grid-demo-bg1">&nbsp;</div>
    </div>
  </div>		  
          <!-- 注意:如果你直接复制所有代码到本地,上述js路径需要改成你本地的 -->
<script>
layui.use(['form', 'upload','element'], function(){
  var $ = layui.jquery
  ,upload = layui.upload
  ,element = layui.element;
element.init();
var is =0
  //多文件列表示例
   var demoListView = $('#demoList')
  ,uploadListIns = upload.render({
    elem: '#testList'
    ,url: 'http://127.0.0.1:8080/file' //改成您自己的上传接口
    ,accept: 'file'
    ,multiple: true
    ,auto: false
    ,bindAction: '#testListAction'
    ,choose: function(obj){   
      var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
      //读取本地文件
	  is=is+1
      obj.preview(function(index, file, result){
        var tr = $(['<tr id="upload-'+ index +'">'
          ,'<td>'+ file.name +'</td>'
          //,'<td>'+ (file.size/1024).toFixed(1) +'kb</td>'
          ,'<td><div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="progressBar'+is+'">\
			<div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>\
		</div></td>'
          ,'<td>等待上传</td>'
          ,'<td>'
            ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
            ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
          ,'</td>'
        ,'</tr>'].join(''));
       
        //单个重传
        tr.find('.demo-reload').on('click', f

评论

刷新

友情链接