์๋
ํ์ญ๋๊น,
์ฝ๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋ด ์๋น์ค๊ฐ ๋ชจ๋ ๊ฒ์ application/json์ผ๋ก ๋ณํํ๋ ๊ฒ์ ๋ฉ์ถ ์ ์์ต๋๋ค.
๋๋ ์๋ํ๋ค:
services.AddMvc(config => {config.RespectBrowserAcceptHeader = true;});
[Produce("video/mp4")]๋ก ์๋ํ์ง๋ง "์ถ๋ ฅ ํฌ๋งทํฐ๊ฐ ์์ต๋๋ค"๋ผ๊ณ ํ์๋ฉ๋๋ค.
์ฌ์ ํ ์๋ฌด๊ฒ๋ ๊ฒฐ๊ณผ:
์ ํ๋ฆฌ์ผ์ด์
/json
๋๋
406 ํ์ฉ๋์ง ์์
.net core 1.0์์ ์ด๋ป๊ฒ ์๋ํฉ๋๊น?
์ปจํธ๋กค๋ฌ/์์ ์ ์ผ๋ถ ์ฝ๋๋ฅผ ํฌํจํ์ฌ ์ํํ๋ ค๋ ์์ ์ ๋ํ ์ถ๊ฐ ์ ๋ณด๊ฐ ํ์ํฉ๋๋ค.
์ก์
๋ฉ์๋์์ 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: ๊ฐ์ฌํฉ๋๋ค :)
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์๋์.
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); }