上傳文件

//上傳文件
func (this *ObjectController) UploadFiles() {
    url, _ := base.GetApiLink(this.Ctx.Request)
    result := base.NewApiResult(Version, url)
    this.Data["json"] = result
    fileKey := "file"
    //獲取文件
    f, fh, err := this.GetFile(fileKey)
    if err != nil {
        result.GenerateErrorApiResult(fmt.Sprintf("獲取文件錯誤,錯誤緣由爲:[ %s ]", err.Error()))
        beego.Error(result.Error.Msg)
        this.ServeJSON()
        return
    }
 
    // 創建臨時處理目錄
    tmpDir := filepath.Join("plugin/template/upload")
    base.Mkdirs([]string{tmpDir})
    beego.Debug(tmpDir)
    defer f.Close()
    defer func() {
        err = os.RemoveAll("plugin/template/upload")
        if err != nil {
            beego.Error(err)
        }
    }()
 
    //上傳目標文件
    destFile := filepath.Join(tmpDir, fh.Filename)
    err = this.SaveToFile(fileKey, destFile)
    if err != nil {
        result.GenerateErrorApiResult(fmt.Sprintf("上傳目標文件錯誤,錯誤緣由爲:[ %s ]", err.Error()))
        beego.Error(result.Error.Msg)
        this.ServeJSON()
        return
    }
 
    count, err := cpt.Import(destFile)
    if err != nil {
        result.GenerateErrorApiResult(fmt.Sprintf("導入文件失敗,錯誤緣由爲:[ %s ]", err.Error()))
        beego.Error(result.Error.Msg)
        this.ServeJSON()
        return
    }
    data := make(map[string]interface{})
    data["items"] = count
    result.GenerateSuccessApiResult(data)
    beego.Debug("上傳文件參數:", destFile)
    this.ServeJSON()
}
相關文章
相關標籤/搜索