user8555937
user8555937

Reputation: 2387

Javascript: changing input file type?

I have a simple HTML file input:

<input type="file" id="file" onchange="__func">

Once I load the file in the input, I call __func:

function __func(event)
{
    let file = event.target.files[0];
        file.type = 'text/csv';
}

Problem is when I attempt to change the file type within __func, either type of file does not change, either I receive the error:

TypeError: "setting getter-only property "type""

How can I dynamically change file type using javascript? I need to do it because I'm sending the file via a FormData wrapper, and because it's plain CSV the content type is not properly detected and set to application/octet-stream automatically.

Upvotes: 2

Views: 1098

Answers (1)

lry
lry

Reputation: 748

You can use slice method to create a new File Blob.

const blob = file.slice(0, file.size, "text/csv")

Upvotes: 3

Related Questions