Skip to content

How to get file from postman #150

Description

@surapong-tho

I'm newbie a developer
How to get image from form data type file from api(postman)

Activity

  1. surapong-tho commented on Oct 12, 2021

    @surapong-tho
    Author
    log := logger.WithPrefixFunctionName()
    file, err := c.FormFile("file")
    if err != nil {
    	log.CErrorln(c,"Get file error, ", err)
    	return err
    }
    log.Println(file)
    //image, err := file.Open()
    //if err != nil {
    //	log.CErrorln(c, "Open file error, ", err)
    //	return err
    //}
    src, err := imaging.Open(file.Filename)
    if err != nil {
    	log.CErrorln(c, "Open file error, ", err)
    	return err
    }
    
    src = imaging.Resize(src, 200, 0, imaging.Lanczos)
    
    // Save the resulting image as JPEG.
    err = imaging.Save(src, "testdata/out_example.jpg")
    if err != nil {
    	log.CErrorln(c, "failed to save image: %v", err)
    	return err
    }
    return nil
    

    this is my code thx

  2. disintegration commented on Oct 24, 2021

    @disintegration
    Owner

    Hi,

    What type is c in your example?

    file, err := c.FormFile("file")
    

    The FormFile method of http.Request from standard library returns a multipart.File (https://pkg.go.dev/net/http#Request.FormFile). You can pass it to imaging.Decode to decode the image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions