2

我正在创建一个 WinJS 应用程序并使用 XMLHttpRequest 将照片作为 blob 发送到 Flask 网络服务器。

openPicker.pickSingleFileAsync().then(function (file) {
        file.openAsync(Windows.Storage.FileAccessMode.read).done(function (stream) {
            var blob = MSApp.createBlobFromRandomAccessStream("application/octet-stream", stream);
            var fdata = new FormData();
            fdata.append("file", blob, "photo.jpg");

            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("POST", "http://127.0.0.1:5000/api/addPhoto", true);
            xmlhttp.setRequestHeader("Content-type", "multipart/form-data");
            xmlhttp.send(fdata);
        });
    });

这会产生以下 HTTP 请求:

POST http://127.0.0.1:5000/api/addPhoto HTTP/1.1
Accept: */*
Content-Type: application/octet-stream, multipart/form-data; boundary=---------------------------7dd2a320aa0ec0
Accept-Language: en-US,en;q=0.7,ja;q=0.3
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; MSAppHost/1.0)
Host: 127.0.0.1:5000
Content-Length: 9471100
Connection: Keep-Alive
Pragma: no-cache

-----------------------------7dd2a320aa0ec0
Content-Disposition: form-data; name="file"; filename="photo.jpg"
Content-Type: application/octet-stream

处理 Flask Web 服务器上的请求

UPLOAD_FOLDER = '/images'
@app.route('/api/addPhoto', methods=['POST'])
def addPhoto():
        if request.method == 'POST':
            f = request.files['file']
            if f and allowed_file(f.filename):
                    return "error" #add error response here
        else:
                    filename = secure_filename(f.filename)
                    f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                    f.close
                    return "ok" #add success response here

我收到以下错误:

TypeError: 'ImmutableMultiDict' object is not callable

我有几个问题找不到答案:

  • 我是否以正确的格式发送数据?我是否正确地将数据附加到我的表单中?
  • 我的 HTTP 内容类型是否正确?
  • 我是否试图错误地从 HTTP 请求中提取文件?

谢谢!

4

1 回答 1

3

尝试替换它:

f = request.files('file')

和:

f = request.files['file']
于 2013-04-14T19:33:55.677 回答