blog

画像やエクセルシートをダウンロードするためのタグシミュレーション

ストリームを直接ダウンロードすると、バックエンドは取得リクエストの URL を返します。\nストリームの直接ダウンロード\nエクスポートコンスト = => {\n を返します。...

Jan 2, 2021 · 2 min. read
シェア
  1. データストリームを直接ダウンロードし、バックエンドは取得リクエストのURLアドレスを返します。

データストリームの直接ダウンロード

export const ExportMemberData = (taskId,status) => {
 return `${API_PATH}/liz-qy-admin/authsec/task/group/msg/customers/user/export/${taskId}/${status}`
}
var a = document.createElement('a');
a.style.display = 'none';
a.download = 'download';
a.href = ExportMemberData(id,status);
a.click();

直接ダウンロードURL

//ExportManageGroupList POST 
//表をエクスポートする
 handleExport = () => {
 const { searchParmas, stop } = this.state
 if (stop) return
 this.setState({
 stop: true
 }, () => {
 ExportManageGroupList(searchParmas).then(res => {
 if (res.code == 100) {
 var a = document.createElement('a');
 a.style.display = 'none';
 a.download = 'download';
 a.href = res.data;
 a.click();
 this.setState({ stop: false })
 } else {
 throw res
 }
 }).catch(req => {
 this.setState({ stop: false })
 message.error(req.msg ? req.msg : 'サーバーエラー。')
 })
 })
 }

ジャンプダウンロード

downloadQrCode = (record)=>{
 let a = document.createElement('a')
 a.target = '_blank'
 a.href = record.staticUrl + '?d'
 a.click()
}

小塊

export const downloadImage = (imgUrl, download) => { function toDataURL(url) { return fetch(url).then((response) => { return response.blob(); }).then(blob => { return URL.createObjectURL(blob); }); } (async function() { const a = document.createElement("a"); imgUrl = imgUrl+'?d='+Math.random() a.href = await toDataURL(imgUrl); a.setAttribute('download', download ? download:'pic.png') a.click(); })() }
Read next

「Promise, setTimeout, async, await」である。

エンカウンター、マクロタスクキューの次のラウンドにスロー。Eventloopの現在のラウンドが完了するのを待ってから、2ラウンド目の実行へ。 新しいPromiseに出会い、中の非同期処理はすぐに実行されますが、その中のコールバック関数はマイクロタスクキューにスローされます。thenの前のPromiseインスタンス内の非同期操作は完了していなければならないことに注意してください...

Jan 2, 2021 · 3 min read