本文介绍了如何从Blob创建File对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! DataTransferItemList.add 允许您覆盖javascript中的复制操作。但是,它只接受文件对象。DataTransferItemList.add allows you to override copy operation in javascript. It, however, only accepts File object.我的复制事件中的代码:var items = (event.clipboardData || event.originalEvent.clipboardData);var files = items.items || items.files;if(files) { var blob = Blob.fromDataURL(_this.editor.selection.getSelectedImage().toDataURL("image/png")); files.add(blob);} chrome中的错误:The error in chrome: 尝试新文件(Blob blob,DOMString名称) 在谷歌浏览器中我试过这个,根据当前规范:var blob = Blob.fromDataURL(_this.editor.selection.getSelectedImage().toDataURL("image/png"));var file = new File(blob, "image.png");问题在于,Google Chrome不会严格遵守规格。Problem here is, that Google Chrome doesn't stick to specifications very much.在这种情况下,Firefox也不存在:Neither does Firefox in this case: 尝试新文件([Mixed blobParts],DOMString名称,BlobPropertyBag选项) @apsillers建议的解决方案也不起作用。这是在 Firefox 中使用(但无用)的非标准方法和 Chrome 。Trying the new File([Mixed blobParts], DOMString name, BlobPropertyBag options)Solution suggested by @apsillers doesn't work too. This is non stadard method used (but useless) in both Firefox and Chrome.我试图避免使用blob,但文件构造函数无论如何都失败了:I tried to avoid blob, but the file constructor failed anyway: //Canvas to binary var data = atob( //atob (array to binary) converts base64 string to binary string _this.editor.selection.getSelectedImage() //Canvas .toDataURL("image/png") //Base64 URI .split(',')[1] //Base64 code ); var file = new File([data], "image.png", {type:"image/png"}); //ERROR您可以在控制台中尝试:You can try that in console: Chrome< 38 : Chrome> = 38 : Firefox :Chrome <38:Chrome >=38:Firefox:传递 Blob 是可能正确并适用于Firefox:Passing Blob is probably correct and works in Firefox:var file = new File([new Blob()], "image.png", {type:"image/png"}); Firefox: Chrome< 38 : Chrome> = 38 :Firefox:Chrome <38:Chrome >=38: 问:那么如何从 Blob 文件 >?Q: So how can I make File from Blob? 注意:我在@apsillers提醒我更新Google Chrome后添加了更多屏幕截图。Note: I added more screenshots after @apsillers reminded me to update Google Chrome.推荐答案文件构造函数(以及Blob构造函数)接受一系列部件。部件不必是DOMString。它也可以是Blob,File或类型化数组。您可以像这样轻松地从Blob构建文件:The File constructor (as well as the Blob constructor) takes an array of parts. A part doesn't have to be a DOMString. It can also be a Blob, File, or a typed array. You can easily build a File out of a Blob like this: 新文件([blob],filename)请不要声明浏览器没有实现规范,或者如果您没有花时间了解规范流程或规范本身。Please refrain from stating that browsers don't implement the spec or that the spec is useless if you don't take the time to understand the spec process or the spec itself. 这篇关于如何从Blob创建File对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 16:09