C#의 리소스 핸들러 헤더의 이름 변경
-
05-07-2019 - |
문제
QueryString을 통해 전달 된 매개 변수를 기반으로 Response.writeFile (Filename) 인 리소스 처리기가 있습니다. Mimetype을 올바르게 처리하고 있지만 문제는 일부 브라우저에 있으며 Filename은 Mypdf.pdf (출력중인 파일) 대신 Res.ASHX (핸들러 이름)로 나타납니다. 파일 이름이 서버로 다시 전송 될 때 파일 이름을 변경하는 방법을 알려줄 수 있습니까? 내 코드는 다음과 같습니다.
// Get the name of the application
string application = context.Request.QueryString["a"];
string resource = context.Request.QueryString["r"];
// Parse the file extension
string[] extensionArray = resource.Split(".".ToCharArray());
// Set the content type
if (extensionArray.Length > 0)
context.Response.ContentType = MimeHandler.GetContentType(
extensionArray[extensionArray.Length - 1].ToLower());
// clean the information
application = (string.IsNullOrEmpty(application)) ?
"../App_Data/" : application.Replace("..", "");
// clean the resource
resource = (string.IsNullOrEmpty(resource)) ?
"" : resource.Replace("..", "");
string url = "./App_Data/" + application + "/" + resource;
context.Response.WriteFile(url);
해결책
Joel의 의견에서 연장되면 실제 코드는 다음과 같습니다.
context.Response.AddHeader("content-disposition", "attachment; filename=" + resource);
다른 팁
이 Scott Hanselman 게시물은 도움이 될 것입니다.
http://www.hanselman.com/blog/commentview.aspx?guid=360
답변 해 주셔서 감사합니다. 최종 코드는 PDF의 작동 및 확인입니다.
if (extensionArray[extensionArray.Length - 1].ToLower() == "pdf")
context.Response.AddHeader("content-disposition",
"Attachment; filename=" + resource);
제휴하지 않습니다 StackOverflow