Golang Request.ParseMultipartForm Examples

Golang Request.ParseMultipartForm - 30 examples found. These are the top rated real world Golang examples of net/http.Request.ParseMultipartForm extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
// for simple values it will use straight method calls
func (o *FindParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
	var res []error
	if err := r.ParseMultipartForm(32 << 20); err != nil {
		if err != http.ErrNotMultipart {
			return err
		} else if err := r.ParseForm(); err != nil {
			return err
		}
	}
	fds := httpkit.Values(r.Form)

	if err := o.bindXRateLimit(r.Header["X-Rate-Limit"], true, route.Formats); err != nil {
		res = append(res, err)
	}

	fdLimit, fdhkLimit, _ := fds.GetOK("limit")
	if err := o.bindLimit(fdLimit, fdhkLimit, route.Formats); err != nil {
		res = append(res, err)
	}

	fdTags, fdhkTags, _ := fds.GetOK("tags")
	if err := o.bindTags(fdTags, fdhkTags, route.Formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}
Example #2
0
// ReadRequestOneFile reads the first file from the request (if multipart/),
// or returns the body if not
func ReadRequestOneFile(r *http.Request) (body io.ReadCloser, contentType string, status int, err error) {
	body = r.Body
	contentType = r.Header.Get("Content-Type")
	Log.Debug("ReadRequestOneFile", "ct", contentType)
	if !strings.HasPrefix(contentType, "multipart/") {
		// not multipart-form
		status = 200
		return
	}
	defer r.Body.Close()
	err = r.ParseMultipartForm(1 << 20)
	if err != nil {
		status, err = 405, errors.New("error parsing request as multipart-form: "+err.Error())
		return
	}
	if r.MultipartForm == nil || len(r.MultipartForm.File) == 0 {
		status, err = 405, errors.New("no files?")
		return
	}

Outer:
	for _, fileHeaders := range r.MultipartForm.File {
		for _, fileHeader := range fileHeaders {
			if body, err = fileHeader.Open(); err != nil {
				status, err = 405, fmt.Errorf("error opening part %q: %s", fileHeader.Filename, err)
				return
			}
			contentType = fileHeader.Header.Get("Content-Type")
			break Outer
		}
	}
	status = 200
	return
}
Example #3
0
func addItem(w http.ResponseWriter, q *http.Request) {
	//check if SKU exist, if not then generate SKU
	err := q.ParseMultipartForm(32 << 20)

	if err != nil {
		// Handle error
		fmt.Println(err)
	}
	if q.FormValue("SKU") != "" {

		slice := []string{"Image", "Image1", "Image2", "Image3"}

		images := checkifImageneedUpload(q, slice, q.FormValue("SKU"))

		product := Product{
			Title:       q.FormValue("Title"),
			Price:       q.FormValue("Price"),
			SKU:         q.FormValue("SKU"),
			Image:       images,
			Description: q.FormValue("Description"),
		}

		db := dbinit()
		defer db.Close()
		_, err = r.Db("goShop").Table("items").Insert(product).RunWrite(db)
		if err != nil {
			log.Println(err)
		}

		//fmt.Println(q.FormValue("cat"))

		http.Redirect(w, q, "/items", http.StatusFound)
	}

}
Example #4
0
func uploadfunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		w.Write([]byte("hello world from host1" + r.Method + "\n"))
		return
	}
	r.ParseMultipartForm(1 << 20)
	//w.Write([]byte("hello world from host1"))
	//w.Write([]byte(r.Header.Get("Content-Type")))
	f, fh, err := r.FormFile("upload")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer f.Close()
	filename := fh.Filename
	if strings.Contains(fh.Filename, ":") {
		strtmp := strings.Split(fh.Filename, ":")
		filename = strtmp[len(strtmp)-1]
		fmt.Fprintf(w, "\nchange file name:%s\n", filename)
	} else if strings.Contains(fh.Filename, "/") {
		strtmp := strings.Split(fh.Filename, "/")
		filename = strtmp[len(strtmp)-1]
		fmt.Fprintf(w, "\nchange file name:%s\n", filename)
	}
	fmt.Fprintf(w, "\n%+v\n%s\n", fh.Header, filename)
	file, err := os.OpenFile("/tmp/"+filename, os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()
	io.Copy(file, f)
}
Example #5
0
func (sh *SetupHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	if !auth.LocalhostAuthorized(req) {
		fmt.Fprintf(rw,
			"<html><body>Setup only allowed from localhost"+
				"<p><a href='/'>Back</a></p>"+
				"</body></html>\n")
		return
	}
	if req.Method == "POST" {
		err := req.ParseMultipartForm(10e6)
		if err != nil {
			httputil.ServerError(rw, req, err)
			return
		}
		if len(req.Form) > 0 {
			handleSetupChange(rw, req)
			return
		}
		if strings.Contains(req.URL.Path, "restartCamli") {
			err = osutil.RestartProcess()
			if err != nil {
				log.Fatal("Failed to restart: " + err.Error())
			}
		}
	}

	sendWizard(rw, req, false)
}
Example #6
0
////////////////////////////////
// Upload files
////////////////////////////////
func uploads(w http.ResponseWriter, r *http.Request) {
	type file struct {
		Name string
	}

	if r.Method == "GET" {
		filez, _ := ioutil.ReadDir("./uploads/")
		files := []file{}
		for _, fn := range filez {
			f := file{}
			f.Name = fn.Name()
			files = append(files, f)
		}
		t.ExecuteTemplate(w, "header", v)
		t.ExecuteTemplate(w, "upload", files)
	} else {
		r.ParseMultipartForm(32 << 20)
		file, handler, err := r.FormFile("uploadfile")
		if err != nil {
			fmt.Println(err)
			return
		}
		defer file.Close()
		f, err := os.OpenFile("./uploads/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
		if err != nil {
			fmt.Println(err)
			return
		}
		defer f.Close()
		io.Copy(f, file)
		http.Redirect(w, r, "/kayden/upload", 302)
	}
}