dqy1265 2017-11-21 14:38
浏览 34

将Dropzone发布文件反应到Go服务器

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看