如何在 JavaScript 中将 Blob 转换为文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27159179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:48:17  来源:igfitidea点击:

How to convert Blob to File in JavaScript

javascriptnode.jsfile-uploadblob

提问by skip

I need to upload an image to NodeJS server to some directory. I am using connect-busboynode module for that.

我需要将图像上传到 NodeJS 服务器到某个目录。我正在connect-busboy为此使用节点模块。

I had the dataURLof the image that I converted to blob using the following code:

dataURL使用以下代码将图像转换为 blob:

dataURLToBlob: function(dataURL) {
    var BASE64_MARKER = ';base64,';
    if (dataURL.indexOf(BASE64_MARKER) == -1) {
        var parts = dataURL.split(',');
        var contentType = parts[0].split(':')[1];
        var raw = decodeURIComponent(parts[1]);
        return new Blob([raw], {type: contentType});
    }
    var parts = dataURL.split(BASE64_MARKER);
    var contentType = parts[0].split(':')[1];
    var raw = window.atob(parts[1]);
    var rawLength = raw.length;
    var uInt8Array = new Uint8Array(rawLength);
    for (var i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
    }
    return new Blob([uInt8Array], {type: contentType});
}

I need a way to convert the blob to a file to upload the image.

我需要一种方法将 blob 转换为文件以上传图像。

Could somebody help me with it?

有人可以帮我吗?

回答by Joshua P Nixon

You can use the File constructor:

您可以使用 File 构造函数:

var file = new File([myBlob], "name");

As per the w3 specification this will append the bytes that the blob contains to the bytes for the new File object, and create the file with the specified name http://www.w3.org/TR/FileAPI/#dfn-file

根据 w3 规范,这会将 blob 包含的字节附加到新 File 对象的字节中,并创建具有指定名称http://www.w3.org/TR/FileAPI/#dfn-file的文件

回答by CBarr

This function converts a Blobinto a Fileand it works great for me.

此函数将 a 转换Blob为 a File,对我来说效果很好。

Vanilla JavaScript

原生 JavaScript

function blobToFile(theBlob, fileName){
    //A Blob() is almost a File() - it's just missing the two properties below which we will add
    theBlob.lastModifiedDate = new Date();
    theBlob.name = fileName;
    return theBlob;
}

TypeScript(with proper typings)

TypeScript(具有正确的类型)

public blobToFile = (theBlob: Blob, fileName:string): File => {
    var b: any = theBlob;
    //A Blob() is almost a File() - it's just missing the two properties below which we will add
    b.lastModifiedDate = new Date();
    b.name = fileName;

    //Cast to a File() type
    return <File>theBlob;
}

Usage

用法

var myBlob = new Blob();

//do stuff here to give the blob some data...

var myFile = blobToFile(myBlob, "my-image.png");

回答by mili

Joshua P Nixon's answer is correct but I had to set last modified date also. so here is the code.

Joshua P Nixon的回答是正确的,但我也必须设置上次修改日期。所以这是代码。

var file = new File([blob], "file_name", {lastModified: 1534584790000});

1534584790000is an unix timestamp for "GMT: Saturday, August 18, 2018 9:33:10 AM"

1534584790000是Unix时间戳“ GMT:星期六,2018年8月18日上午09点33分十秒

回答by DAVID _

My modernvariant:

我的现代变体:

function blob2file(blobData) {
  const fd = new FormData();
  fd.set('a', blobData);
  return fd.get('a');
}

回答by Alexandre Daubricourt

For me to work I had to explicitly provide the typealthough it is contained in the blob by doing so:

为了让我工作,我必须明确提供类型,尽管它包含在 blob 中:

const file = new File([blob], 'untitled', { type: blob.type })

回答by Moslem Shahsavan

Use saveAson FileSaver.jsgithub project.

使用saveAsFileSaver.jsgithub上的项目。

FileSaver.jsimplements the saveAs()FileSaver interface in browsers that do not natively support it.

FileSaver.jssaveAs()在本机不支持它的浏览器中实现FileSaver 接口。