๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

AJAX๋ฅผ ์ด์šฉํ•œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ์‹œ ํ•œ๊ธ€ ํŒŒ์ผ๋ช… ๊นจ์ง ๋ฌธ์ œ ํ•ด๊ฒฐํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ’ป Java Study/Java ์ด๋ก  ์ •๋ฆฌ

AJAX๋ฅผ ์ด์šฉํ•œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ์‹œ ํ•œ๊ธ€ ํŒŒ์ผ๋ช… ๊นจ์ง ๋ฌธ์ œ ํ•ด๊ฒฐํ•˜๊ธฐ

jhj.sharon 2024. 7. 25. 14:39
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์ƒํ™ฉ ์„ค๋ช…

์ตœ๊ทผ ํ”„๋กœ์ ํŠธ์—์„œ AJAX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•  ๋•Œ ํ•œ๊ธ€ ํŒŒ์ผ๋ช…์ด ๊นจ์ง€๋Š” ํ˜„์ƒ์„ ๊ฒช์—ˆ๋‹ค. ๋™์ผํ•œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ๊ธฐ๋Šฅ์„ form ์ œ์ถœ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ๋Š” ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜๋‹ค.

  • 882ECA99EEB84A7ECB18FEDB5B3%84_240715-240726.xlsx -> ์ด๋Ÿฐ์‹์œผ๋กœ ํ•œ๊ธ€ ๊นจ์ง

 

์›์ธ ๋ถ„์„

AJAX์™€ form ์ œ์ถœ ๋ฐฉ์‹์˜ ์ฐจ์ด์ 

  • Form ์ œ์ถœ ๋ฐฉ์‹: ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋‹ค์šด๋กœ๋“œ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•œ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๋Š” Content-Disposition ํ—ค๋”๋ฅผ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ , ํŒŒ์ผ๋ช… ์ธ์ฝ”๋”ฉ์„ ์•Œ์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋”ฐ๋ผ์„œ Form ํ˜•์‹์˜ ํŒŒ์ผ๋‹ค์šด๋กœ๋“œ์—์„œ๋Š” ํŒŒ์ผ๋ช…์ด ์ œ๋Œ€๋กœ ์ถœ๋ ฅ๋ฌ๋‹ค.
  • AJAX ๋ฐฉ์‹: XMLHttpRequest ๋˜๋Š” Fetch API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ์ ์œผ๋กœ ํŒŒ์ผ์„ ์š”์ฒญํ•˜๊ณ , JavaScript๋กœ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ธฐ๋ณธ ๋‹ค์šด๋กœ๋“œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ํŒŒ์ผ๋ช… ์ธ์ฝ”๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜๋™์œผ๋กœ ํ•ด์•ผ ํ•œ๋‹ค. AJAX ๋ฐฉ์‹์—์„œ๋Š” ์„œ๋ฒ„์—์„œ ๋ณด๋‚ด๋Š” Content-Dispositionํ—ค๋”๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ถ”๊ฐ€์ ์ธ ์ธ์ฝ”๋”ฉ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

Content-Disposition ํ—ค๋” ์ฒ˜๋ฆฌ ๋ฐฉ์‹

  • Content-Disposition ํ—ค๋”๋Š” ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ์‹œ ํŒŒ์ผ๋ช…์„ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ์ด ํ—ค๋”๋Š” ASCII ๋ฌธ์ž๋งŒ์„ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๊ธ€ ํŒŒ์ผ๋ช…์€ ์ธ์ฝ”๋”ฉ๋˜์–ด ์ „๋‹ฌ๋˜์–ด์•ผ ํ•œ๋‹ค.
  • RFC 5987 ํ‘œ์ค€์„ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ผ๋ช…์„ UTF-8๋กœ ์ธ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ** RFC 5987์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์„ค๋ช… : RFC 5987์€ HTTP ํ—ค๋” ํ•„๋“œ์— ๊ตญ์ œํ™”๋œ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ •์˜ํ•œ ํ‘œ์ค€์ด๋‹ค. ์ด ํ‘œ์ค€์„ ๋”ฐ๋ฅด๋ฉด, UTF-8๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์„ ํ—ค๋” ํ•„๋“œ์— ํฌํ•จ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

ํด๋ผ์ด์–ธํŠธ ์ธก ์ฝ”๋“œ ์ˆ˜์ • ๋‚ด์šฉ

  • ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ํŒŒ์ผ๋ช…์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ธ์ฝ”๋”ฉํ•˜์—ฌ ์ฒ˜๋ฆฌ

์„œ๋ฒ„ ์ธก ์ฝ”๋“œ ์ˆ˜์ • ๋‚ด์šฉ

  • ์„œ๋ฒ„ ์ธก์—์„œ๋Š” Content-Disposition ํ—ค๋”์— ํŒŒ์ผ๋ช…์„ UTF-8๋กœ ์ธ์ฝ”๋”ฉํ•˜์—ฌ ์„ค์ •

 

์ฝ”๋“œ ์˜ˆ์‹œ

1. ๋ฌธ์ œ๊ฐ€ ์žˆ๋˜ ์›๋ž˜ ์ฝ”๋“œ

ํด๋ผ์ด์–ธํŠธ ์ธก ์ฝ”๋“œ

fetch('/download', {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json',
    },
})
.then(response => response.blob())
.then(blob => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = 'ํŒŒ์ผ๋ช….xlsx'; // ํ•œ๊ธ€ ํŒŒ์ผ๋ช… ๊นจ์ง
    document.body.appendChild(a);
    a.click();
    a.remove();
});

์„œ๋ฒ„ ์ธก ์ฝ”๋“œ

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=\"ํŒŒ์ผ๋ช….xlsx\"");

 

2. ์ˆ˜์ •๋œ ์ฝ”๋“œ

ํด๋ผ์ด์–ธํŠธ ์ธก ์ฝ”๋“œ

fetch('/download', {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json',
    },
})
.then(response => {
    const disposition = response.headers.get('Content-Disposition');
    const filename = disposition.match(/filename\*?=['"]?([^;\n"]*)['"]?/)[1];
    const decodedFilename = decodeURIComponent(filename.replace(/\+/g, ' '));
    return response.blob().then(blob => {
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = decodedFilename;
        document.body.appendChild(a);
        a.click();
        a.remove();
    });
});

์„œ๋ฒ„ ์ธก ์ฝ”๋“œ

import org.apache.commons.codec.net.URLCodec;

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
String fileName = "ํŒŒ์ผ๋ช….xlsx";
URLCodec codec = new URLCodec();
String encodedFileName = codec.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);

 

 

 

์ฐธ๊ณ  ์ž๋ฃŒ

๊ด€๋ จ RFC ๋ฌธ์„œ ๋งํฌ

  • RFC 5987: HTTP ํ—ค๋” ํ•„๋“œ์— ๋Œ€ํ•œ ๊ตญ์ œํ™”๋œ ๋ฌธ์ž์—ด ์ง€์›

๊ธฐํƒ€ ๋„์›€์ด ๋œ ์ž๋ฃŒ๋“ค

๋ฐ˜์‘ํ˜•
Comments