본문 바로가기

프로그래밍/C#

[C#] 파일 다운로드 한글 깨짐 방지

반응형

한글로된 파일 다운로드할 때 한글 깨짐 현상 방지



public ActionResult Down(string txtFName, string pathURL, string compareHash)
{
    try
    {
        string filePath = Server.MapPath(pathURL + txtFName);
 
        string chkFilehash = cls_Common.gf_FileCompareHashData(txtFName);
 
        if (chkFilehash != compareHash)
        {
            return Content(cls_Common.JScriptMsgRedirect(new string[] { "다운로드 파일이 위, 변조 되었습니다.", "history.back(-1);" }));
        }
 
        if (!System.IO.File.Exists(filePath))
        {
            return Content(cls_Common.JScriptMsgRedirect(new string[] { "파일이 없습니다.", "history.back(-1);" }));
        }
 
        //한글파일 깨짐 방지
        string enCodeFileName = Server.UrlEncode(txtFName);
        Response.AddHeader("content-disposition", "attachment; filename=" + enCodeFileName);
 
        return File(pathURL + txtFName, "application/unknown", txtFName);
    }
    catch (Exception err)
    {
        return new HttpNotFoundResult();
    }
}

반응형