2

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.

1 Answer 1

3

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

const blob = file.slice(0, file.size, "text/csv")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.