2017-12-03 64 views
0

Azure函數HTTP綁定從Azure Blob存儲讀取圖像作爲Base64字符串。如何將Azure函數的base64圖像作爲二進制數據返回

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQTEhIUEhIUFBUV…K9rk8hCAEkjFMUYiEAI+nHIpsQh0AkisDYRTOiCAbWVtgCtI6IlkHh7LDTQXLH0EIQBj//2Q== 

它把使用新的緩衝區它:

const buf = new Buffer(pictureObj.data.split(",")[1], "base64"); 

然後返回此緩衝區是這樣的:

context.bindings.res = { 
    "status": 200, 
    "headers": { 
     "Content-Type": type || "image/jpeg" 
    }, 
    "body": new Uint8Array(buf) 
}; 

可惜,這是行不通的。設置「isRaw」既不工作也不返回緩衝區(buf)本身。錯誤是406(不可接受),並且主體是空的。

現在的問題是:如何通過HTTP輸出綁定將base64作爲二進制圖像返回?

另外,再增加一個報頭(諸如內容長度)失敗,此錯誤:

info: Worker.Node.2a68d094-3858-406b-a0c5-a81497b3436b[0] 
    Worker 2a68d094-3858-406b-a0c5-a81497b3436b malformed message invocationResponse.outputData.data.http.headers: string{k:string} expected 
[03/12/2017 02:44:32] A ScriptHost error has occurred 
[03/12/2017 02:44:32] Error: Choose either to return a promise or call 'done'. Do not use both in your script. 
[03/12/2017 02:44:32] Error: Choose either to return a promise or call 'done'. Do not use both in your script. 
+0

用application/octet-stream content-type進行實驗,結果是一樣的。 未設置content-type以base64字符串形式返回緩衝區。 –

回答

1

如果使用天青功能測試這應該工作:

context.res.setHeader("Content-Type", "image/jpeg") 
context.res.raw(new Uint8Array(buf)) 

而且使用時原始的或發送的,因爲它被隱式調用,所以不需要調用context.done。

+0

太棒了。這對beta中的已部署函數有效,但不適用於本地模擬器。這可能是因爲我還沒有想到如何在本地模擬器上啓用測試版功能。 –

+0

請確保你的'npm install -g azure-functions-core-tools @ core'最新版本是2.0.1-beta.22,它應該與Azure功能測試版相匹配 – nelak

+0

這似乎不適用於非beta版本,是有另一種方法嗎? – K48

相關問題