gpt4 book ai didi

javascript - Chrome 中的 FileReader 错误 - JS

转载 作者:行者123 更新时间:2023-11-30 17:30:36 25 4
gpt4 key购买 nike

我正在使用以下 FileReader 脚本尝试将附件上传到 Salesforce 中的对象。

    <label>Select file: <input id="file-input" type="file" name="file" onChange="uploadFile()"></input>


function uploadFile()
{
var f = document.getElementById('file-input').file;
var reader = new FileReader();

// Keep a reference to the File in the FileReader so it can be accessed in callbacks
reader.file = f;

reader.onerror = function(e)
{
switch(e.target.error.code)
{
case e.target.error.NOT_FOUND_ERR:
alert('File Not Found!');
break;
case e.target.error.NOT_READABLE_ERR:
alert('File is not readable');
break;
case e.target.error.ABORT_ERR:
break; // noop
default:
alert('An error occurred reading this file.');
};
};

reader.onabort = function(e)
{
alert('File read cancelled');
};

reader.onload = function(e)
{
var att = new sforce.SObject("Attachment");
att.Name = this.file.name;
att.ContentType = this.file.type;
att.ParentId = parentId;

att.Body = (new sforce.Base64Binary(e.target.result)).toString();

sforce.connection.create([att],
{
onSuccess : function(result, source)
{
if (result[0].getBoolean("success"))
{
console.log("new attachment created with id " + result[0].id);
}
else
{
console.log("failed to create attachment " + result[0]);
}
},
onFailure : function(error, source)
{
console.log("An error has occurred " + error);
}
});
};

reader.readAsBinaryString(f);
}

但我收到以下错误消息:在 Chrome 中:

Uncaught TypeError: Failed to execute 'readAsBinaryString' on 'FileReader': The argument is not a Blob.

有没有人有什么建议?

最佳答案

现代浏览器中文件输入的文件对象是数组的一部分。

你必须改变这一行:

var f = document.getElementById('file-input').file;

到:

var f = document.getElementById('file-input').files[0];

关于javascript - Chrome 中的 FileReader 错误 - JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23150902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com