I't trying to use React-dropzone to post files to a Go backend but am having issues.
This is my react code:
const request = require('superagent');
<Dropzone name="file" onDrop={this.onDrop} multiple={false} />
onDrop = (acceptedFiles) => {
const req = request.post('/uploadTorrent');
acceptedFiles.forEach(file => {
req.attach(file.name, file);
});
req.end();
}
This is the error on the JS side:
XHR failed loading: POST "http://192.168.1.141:8000/uploadTorrent"
POST http://192.168.1.141:8000/uploadTorrent net::ERR_EMPTY_RESPONSE
If I console.log the file variable on the JS side I get this:
File(61718) {preview: "blob:http://192.168.1.141:8000/a3e9ed9f-6f6c-4471-8322-3591e9e138c7", name: "ubuntu-17.04-desktop-amd64.iso (1).torrent", lastModified: 1506018302216, lastModifiedDate: Thu Sep 21 2017 14:25:02 GMT-0400 (Eastern Daylight Time), webkitRelativePath: "", …}
Not sure if that is how it should look or not.
I can CURL the server and it works exactly as expected:
curl http://192.168.1.141:8000/uploadTorrent -F "file=@ubuntu.torrent;filename=ubuntu.torrent"
Here is the code for the Go side:
http.HandleFunc("/uploadTorrent", func(w http.ResponseWriter, r *http.Request) { //grabbing the uploaded Torrent File and adding it to the client
file, header, err := r.FormFile("file")
fmt.Println(reflect.TypeOf(file))
fmt.Println("File: ", file)
if err != nil {
fmt.Println("Error with fetching file or request issue", file)
}
It always pulls in a NIL value for the file variable when I try and upload from JS.
<nil>
File: <nil>
Error with fetching file or request issue <nil>
2017/11/20 23:54:10 http: panic serving 192.168.1.141:64806: runtime error:
invalid memory address or nil pointer dereference
Anyone know what could be causing this?
Update:
I started from scratch with this element:
<form encType="multipart/form-data" method="post" action="/uploadTorrent">
<input name="fileTest" type="file" /><p />
<input type="submit" value="submit" />
</form>
(I edited the backend to look for "fileTest"). This works perfectly. I am forced to do a 301 redirect back to the main page which annoys me since that is not the react way but I am new to this so I will accept this as a workaround I am satisfied with.