Mvc: 406 ํ—ˆ์šฉ๋˜์ง€ ์•Š์Œ [์ฝ˜ํ…์ธ  ์œ ํ˜•: video/mp4]

์— ๋งŒ๋“  2016๋…„ 11์›” 30์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: aspnet/Mvc

์•ˆ๋…•ํ•˜์‹ญ๋‹ˆ๊นŒ,
์•ฝ๊ฐ„์˜ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด ์„œ๋น„์Šค๊ฐ€ ๋ชจ๋“  ๊ฒƒ์„ application/json์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋ฉˆ์ถœ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋‚˜๋Š” ์‹œ๋„ํ–ˆ๋‹ค:

services.AddMvc(config => {config.RespectBrowserAcceptHeader = true;});

[Produce("video/mp4")]๋กœ ์‹œ๋„ํ–ˆ์ง€๋งŒ "์ถœ๋ ฅ ํฌ๋งทํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค"๋ผ๊ณ  ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

์—ฌ์ „ํžˆ ์•„๋ฌด๊ฒƒ๋„ ๊ฒฐ๊ณผ:
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜/json
๋˜๋Š”
406 ํ—ˆ์šฉ๋˜์ง€ ์•Š์Œ

.net core 1.0์—์„œ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๊นŒ?

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

ํ , "๋น„๋””์˜ค/mp4"๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด asp.net ์ฝ”์–ด์—์„œ ๊ตฌํ˜„ํ•œ ์ถœ๋ ฅ ํฌ๋งทํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ?

์•„๋‹ˆ์š”.

MVC Core 1.0์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

C# [HttpGet] public IActionResult Get() { string videoPath = @"D:\sample.mp4"; if (System.IO.File.Exists(videoPath)) { return PhysicalFile(videoPath, "video/mp4", "sample.mp4"); } return StatusCode(StatusCodes.Status500InternalServerError); }

๋ชจ๋“  4 ๋Œ“๊ธ€

์ปจํŠธ๋กค๋Ÿฌ/์ž‘์—…์˜ ์ผ๋ถ€ ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ˆ˜ํ–‰ํ•˜๋ ค๋Š” ์ž‘์—…์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์•ก์…˜ ๋ฉ”์„œ๋“œ์—์„œ IActionResult ๊ฐ€ ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ถœ๋ ฅ ํฌ๋งทํ„ฐ๋ฅผ ํ†ตํ•ด ์‹คํ–‰๋˜์–ด ์‘๋‹ต์— ์“ฐ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ JSON ํฌ๋งทํ„ฐ๊ฐ€ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํ , "๋น„๋””์˜ค/mp4"๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด asp.net ์ฝ”์–ด์—์„œ ๊ตฌํ˜„ํ•œ ์ถœ๋ ฅ ํฌ๋งทํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ?

์ด๊ฒƒ์€ ๋‚ด ์ปจํŠธ๋กค๋Ÿฌ์ž…๋‹ˆ๋‹ค.

        [HttpGet]
        [Produces("video/mp4")]
        public HttpResponseMessage Get()
        {
            string videoPath = @"D:\sample.mp4";
            if (System.IO.File.Exists(videoPath))
            {
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(new FileStream(videoPath, FileMode.Open, FileAccess.Read));
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("video/mp4");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "sample.mp4"
                };
                return response;
            }
                return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }

๋‹จ์ˆœํžˆ owinservice๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋งค์šฐ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

ํ , "๋น„๋””์˜ค/mp4"๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด asp.net ์ฝ”์–ด์—์„œ ๊ตฌํ˜„ํ•œ ์ถœ๋ ฅ ํฌ๋งทํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ?

์•„๋‹ˆ์š”.

MVC Core 1.0์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

C# [HttpGet] public IActionResult Get() { string videoPath = @"D:\sample.mp4"; if (System.IO.File.Exists(videoPath)) { return PhysicalFile(videoPath, "video/mp4", "sample.mp4"); } return StatusCode(StatusCodes.Status500InternalServerError); }

์™„๋ฃŒ :dagger: ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰