图片上传
input file上传文件
File类型数据
url
File转url
const readImageURL = (file: File) => {return new Promise((resolve) => {if (URL) {resolve(URL.createObjectURL(file));} else if (FileReader) {const reader = new FileReader();reader.onload = function () {resolve(reader.result);};reader.readAsDataURL(file);}});};
‘blob:http://localhost:9000/161b1b55-45ed-4e92-ac7c-60e5436c61ff’
blob
url转blob
blob 通过oss api可上传
Axios({url: src,responseType: "blob",}).then((res) => {callback(res.data);});
oss api
支持Buffer.from 和 blob上传
await client.put(objectName,// Buffer.from(JSON.stringify(fileContent))fileContent);
json上传
File
file读成object内存
const reader = new FileReader()reader.readAsText(file)reader.onload = (e) => {let fileContent = nulltry{fileContent = JSON.parse(e.target.result)callback(fileContent)} catch(err) {enqueueSnackbar('JSON parsed failed, please check your file\'s syntax!', {variant: 'error'})return}}
Object
Buffer.from(JSON.stringify(fileContent))
Buffer
await client.put(objectName, Buffer.from(JSON.stringify(fileContent)))
图片下载
const downloadFile = (data: BlobPart[], fileTitle: string) => {const blob = new Blob(data);FileSaver.saveAs(blob, fileTitle);};
