• beego批量上传文件


    最近项目中用到beego,需要实现文件批量上传,翻了好久beego的文档都没有找到满意的解决办法,结果看源码时发现作者已经给出了相关实现代码,在源码包controller.go文件中560-586行,记录如下:

    //GetFiles return multi-upload files
    files, err:=c.GetFiles("myfiles")
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusNoContent)
    		return
    	}
    for i, _ := range files {
    	//for each fileheader, get a handle to the actual file
    	file, err := files[i].Open()
    	defer file.Close()
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    	//create destination file making sure the path is writeable.
    	dst, err := os.Create("upload/" + files[i].Filename)
    	defer dst.Close()
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    	//copy the uploaded file to the destination file
    	if _, err := io.Copy(dst, file); err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    }
    

    对应的input标签需设置multiple 属性

    <input type="file" multiple name="myfiles">
    
  • 相关阅读:
    每周一坑WAF防护域名主节点异常+阿里远程登录不了
    SmsForwarder转发阿里云登录验证码
    python统计IP段 (基础实现版)
    每周一坑nginx日志写不到elk索引
    阿里云WAF本地验证及误拦截处理
    php 1020
    php 1017
    php 1018
    php 1015
    php 1014
  • 原文地址:https://www.cnblogs.com/prince5460/p/12000046.html
Copyright © 2020-2023  润新知