我在Heroku(雪松env)上有一個rails應用程序。它有一個頁面,我使用toDataURL()
方法將畫布數據渲染成圖像。我試圖使用JavaScript(繞過服務器端)將返回的base64圖像數據字符串直接上傳到s3。問題是由於這不是一個文件,我如何直接將base64編碼數據上傳到S3並將其保存爲文件?如何僅使用JavaScript將base64編碼圖像數據上傳到S3?
14
A
回答
23
我已經找到一種方法來做到這一點。經過大量的搜索查看不同的教程。
如果您正在處理多個文件,您必須將數據URI轉換爲blob,然後使用CORS將該文件上載到S3我對每個文件都有單獨的XHR請求。
我發現這個功能,將你的數據URI到一個能夠直接使用CORS(Convert Data URI to Blob)
function dataURItoBlob(dataURI) {
var binary = atob(dataURI.split(',')[1]);
var array = [];
for(var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
}
1
不知道OP是否已經解決了這個問題,但我正在研究一個非常相似的功能。在做一些小小的研究時,我遇到了這些可能有用的文章。
3
被上傳到S3斑點如果有人關心:這裏是coffescript上面給出的函數版本!
convertToBlob = (base64) ->
binary = atob base64.split(',')[1]
array = []
for i in [0...binary.length]
array.push binary.charCodeAt i
new Blob [new Uint8Array array], {type: 'image/jpeg'}
4
Jamcoope的回答非常好,但blob構造函數不被所有瀏覽器支持。最顯着的是android 4.1和android 4.3。有Blob
polyfill,但xhr.send(...)
不適用於polyfill。最好的選擇是這樣的:
var u = dataURI.split(',')[1],
binary = atob(u),
array = [];
for (var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
var typedArray = Uint8Array(array);
// now typedArray.buffer can be passed to xhr.send
相關問題
- 1. 將base64圖像數據上傳到亞馬遜S3
- 2. 將圖像編碼爲Amazon S3的Base64
- 3. 使用AngularJS將正確編碼的(base64?)圖像上傳到SharePoint
- 4. 使用javascript編輯base64編碼圖像
- 5. 如何使用圖像URL將圖像上傳到AWS S3?
- 6. 將圖像上傳到S3
- 7. Swift 3 base64編碼圖像上傳
- 8. 使用javascript將Google Base64圖像上傳到Google驅動器
- 9. 將base64圖像上傳到亞馬遜s3
- 10. 用swift將圖像上傳到S3 3
- 11. 如何將base64編碼數據轉換爲xcode圖像
- 12. 如何正確地將base64編碼圖像上傳到文件服務器?
- 13. 如何使用Perl將圖像上傳到Amazon S3?
- 14. 如何使用GraphQL將圖像上傳到AWS S3?
- 15. 如何使用rails,angular&paperclip將圖像上傳到S3
- 16. 如何使用流星將圖像從FileReader上傳到Amazon s3
- 17. 如何使用php webservice將圖像上傳到Amazon S3?
- 18. 如何使用NodeJS將圖像直接上傳到S3
- 19. 如何使用JavaScript獲取base64編碼圖像的地址
- 20. 使用HttPost和HttpClient上傳base64編碼圖像
- 21. 無法使用Codeigniter上傳base64編碼圖像
- 22. 使用Facebook sdk上傳base64編碼圖像?
- 23. 如何在codeigniter中上傳base64編碼的圖像
- 24. 讓用戶使用JavaScript將圖像上傳到數據庫中
- 25. 如何將base64編碼的圖像傳遞給php?
- 26. 後base64編碼圖像到服務器使用javascript
- 27. 如何上傳圖像到ASW S3
- 28. 如何將圖像傳輸到Amazon S3
- 29. Base64編碼圖像
- 30. Base64編碼圖像