C#でリソースハンドラーのヘッダーの名前を変更する
-
05-07-2019 - |
質問
クエリ文字列を介して渡されたパラメーターに基づいたResponse.WriteFile(fileName)であるリソースハンドラーがあります。 mimetypeを正しく処理していますが、問題は一部のブラウザーで発生します。ファイル名は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