我創建了WinJS應用程序並使用XMLHttpRequest將照片作爲blob發送到Flask網絡服務器。使用XMLHtttpRequest將照片上傳到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
處理瓶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請求錯誤地拉文件?
謝謝!
在哪裏出現了類型錯誤: 'ImmutableMultiDict' 對象不是可調用的?你能發佈完整的堆棧跟蹤嗎?幸運的是,這看起來只是一些Flask/Python錯誤,而不是一些奇怪的HTTP問題,至少要開始。 此外,如果您不使用pdb,這裏可能會有所幫助......只需在頂部輸入pdb,然後在您的處理程序開始處輸入pdb.set_trace(),然後逐步進行調試。 –
您的內容類型標題對我來說似乎是錯誤的,爲什麼它在'multipart/form-data'之前有'application/octet-stream'。 – Musa