Tell me how to send a file to the server golang ajax-ohm?
A similar example is here. How to upload a file to the golang server? , but then the page reloads, and if I try to load ajax-ohm, then the go server can’t be configured to accept the file.
Please tell me how to be?

    1 answer 1

    //подключаем Jquery <form method="POST" id="FormUpload" action="/api/upload" enctype="multipart/form-data"> <input type="file" id="File" name="my-file"> <input type="submit"> </form> <script> $("form#FormUpload").submit(function(event) { event.preventDefault(); var formData = new FormData($(this)[0]); $.ajax({ url: '/api/upload', type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function(returndata) { alert(returndata); } }); return false; });</script> 

    Golang

     func Upload(w http.ResponseWriter, r *http.Request) { file, handler, err := r.FormFile("my-file") if err != nil { fmt.Println(err) return } defer file.Close() f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Println(err) return } defer f.Close() io.Copy(f, file) }