upload files

//upload files
func (this *ObjectController) UploadFiles() {
    url, _ := base.GetApiLink(this.Ctx.Request)
    result := base.NewApiResult(Version, url)
    this.Data["json"] = result
    fileKey := "file"
    // get the file
    f, fh, err := this.GetFile(fileKey)
    if err != nil {
        result.GenerateErrorApiResult(fmt.Sprintf("Error getting file, error reason: [ %s ]", err.Error()))
        beego.Error(result.Error.Msg)
        this.ServeJSON ()
        return
    }
 
    // Create a temporary processing directory
    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)
        }
    }()
 
    //Upload target file
    destFile := filepath.Join(tmpDir, fh.Filename)
    err = this.SaveToFile(fileKey, destFile)
    if err != nil {
        result.GenerateErrorApiResult(fmt.Sprintf("Error uploading target file, error reason: [ %s ]", err.Error()))
        beego.Error(result.Error.Msg)
        this.ServeJSON ()
        return
    }
 
    count, err := cpt.Import(destFile)
    if err != nil {
        result.GenerateErrorApiResult(fmt.Sprintf("Failed to import file, error reason: [ %s ]", err.Error()))
        beego.Error(result.Error.Msg)
        this.ServeJSON ()
        return
    }
    data := make(map[string]interface{})
    data["items"] = count
    result.GenerateSuccessApiResult(data)
    beego.Debug("Upload file parameters:", destFile)
    this.ServeJSON ()
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325207201&siteId=291194637