๋ฐ˜์‘ํ˜•

Axios์—์„œ ์—…๋กœ๋“œ ์ง„ํ–‰ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” onUploadProgress ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ด์šฉํ•ด ์—…๋กœ๋“œ ์ง„ํ–‰ ์ƒํ™ฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฃผ๋กœ ์—…๋กœ๋“œ ์ง„ํ–‰ ์ƒํƒœ๋ฅผ UI๋กœ ํ‘œํ˜„ํ•  ๋•Œ ํ™œ์šฉํ•œ๋‹ค. onUploadProgress ํ•ธ๋“ค๋Ÿฌ๋Š” ProgressEvent ๋„ค์ดํ‹ฐ๋ธŒ ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. ProgressEvent ๊ฐ์ฒด๋Š” ์•„๋ž˜ ์†์„ฑ์„ ํฌํ•จํ•œ๋‹ค. — MDN

 

  1. loaded : ์ด๋ฏธ ์ˆ˜ํ–‰ํ•œ ์ž‘์—…์˜ ์–‘(์—…๋กœ๋“œํ•œ ํฌ๊ธฐ)์„ ๋‚˜ํƒ€๋‚ด๋Š” unsigned long long ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ
  2. total : ์ž‘์—…์˜ ์ด ์–‘(์ด ํŒŒ์ผ ํฌ๊ธฐ)์„ ๋‚˜ํƒ€๋‚ด๋Š” unsigned long long ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ
  3. lengthComputable : ์ง„ํ–‰ ์ƒํ™ฉ ์ธก์ • ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Boolean ๊ฐ’

 

axios.post(api_url, formData, {
  headers: {
    Authorization: 'Bearer ...',
    'Content-Type': 'multipart/form-data',
  },
  onUploadProgress: (progressEvent: ProgressEvent) => {
    const { loaded, total } = progressEvent;
    const percent = (loaded / total) * 100;
    console.log(`total: ${total} | loaded: ${loaded}, percent: ${percent}%`);
  },
});

 

ํŒŒ์ผ ํฌ๊ธฐ๊ฐ€ ํฌ๊ธฐ ์•Š๊ณ  ๋„คํŠธ์›Œํฌ ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค๋ฉด ์ตœ์ดˆ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์„ ์‹œ์ ์— ์ด๋ฏธ ์—…๋กœ๋“œ๊ฐ€ ์™„๋ฃŒ๋œ ์ •๋ณด๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ด๋• ๊ตฌ๊ธ€ ํฌ๋กฌ ๋„คํŠธ์›Œํฌ ํƒญ์—์„œ ๋Š๋ฆฐ 3G ํ™˜๊ฒฝ์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ์ง„ํ–‰ ์ƒํ™ฉ์„ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

๐Ÿ’ก ๋‹ค์šด๋กœ๋“œ ์ง„ํ–‰ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” onDownloadProgress ํ•ธ๋“ค๋Ÿฌ๋„ ์žˆ๋‹ค.

 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•