[vue] js 파일 다운로드, 업로드

Edward Hyun·2022년 10월 6일
0

app&web-dev

목록 보기
155/178

vue에서 사용가능.

// excel Download
const excelFileDownload = async () => {
  // eslint-disable-next-line max-len
  const fileUrl = '파일주소';
  const res = await axios({
    method: 'get',
    url: fileUrl,
    responseType: 'blob',
  });
  const newUrl = window.URL.createObjectURL(res.data);
  const a = document.createElement('a');
  a.href = newUrl;
  a.download = '파일명.확장자';
  a.click();
  a.remove();
  window.URL.revokeObjectURL(newUrl);
};
// excel Upload
const inputFileUpload = ref(); // 태그에 대한, ref
const fileUpload = () => {
  const input = inputFileUpload.value as HTMLInputElement;
  input.click();
};
const readFile = async () => {
  const input = inputFileUpload.value as HTMLInputElement;
  if (input && input.files) {
    const targetFile = input.files[0];
    const formData = new FormData();
    formData.append('file', targetFile);
    const res = await requestApiFunction({
      params: formData
    });
    if (res.isOK) {
    	// ok 처리  
    }
  }
};
profile
앱&웹개발(flutter, vuejs, typescript, react), 인공지능(nlp, asr, rl), 백엔드(nodejs, flask, golang, grpc, webrtc, aws, msa, nft, spring cloud, nest.js), 함수형 프로그래밍(scala, erlang)을 공부하며 정리합니다.

0개의 댓글