上传16进制文件

<input single ref={ref => this.handler = ref} id="icon-button-file" onChange={this.fileChanges}
accept={accept} type="file" style={{ display: 'none' }} />
<label htmlFor="icon-button-file">
<IconButton color="primary" aria-label="upload picture" component="span">
<IconUpload />
</IconButton>
{show&& <Typography variant="h7" component="span" color="primary" gutterBottom>
点击上传
</Typography>}
</label>
 
 
 
fileChanges = (event) => {
let source = event.target.files;
this.setState({ loading: true });
console.log(source)
if (source.length > 0) {
const reader = new FileReader();//
reader.onload = rest => {
const fix = this.buf2hex(rest.target.result);
console.log(fix)
let file = source[0];
let hf = file.name;
const len = hf.lastIndexOf('.');
console.log(len)
hf = hf.substring(len + 1);
console.log(hf)
this.wrapFiles({
value: fix,
key: file.type,
extra: hf,
remark: file.name
});
};
reader.readAsArrayBuffer(source[0]);
}
}
 
 
 
buf2hex = (buffer) => { // buffer is an ArrayBuffer
console.log(buffer)
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
 
 
 
 
wrapFiles = (input) => {
const { onChange } = this.props;
this.setState({ file: input, loading: false });
if (onChange)
onChange(input);
}
posted @ 2020-09-09 09:26  吃鸡小能手  阅读(181)  评论(0编辑  收藏  举报