ダウンロードを促すために Response.TransmitFile() を使用しようとしています。私はこの問題に関する多くの投稿を読み、Rick Strahl のブログ http://www.west-wind.com/weblog/posts/76293.aspxに基づいて方法を調べました。
唯一の違い (私が知る限り) は、仮想ディレクトリの外部にある物理ファイルをターゲットにしていることです。このコードは ajax 化された radgrid で呼び出されます... response.transmitfile が ajax 呼び出しで機能しないのだろうか? ここに私のコードスニペットがあります:
// Get the physical Path of the file
string docFilePath = (string)args.AttachmentKeyValues["DocFilePath"];
// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo file = new FileInfo(docFilePath);
// Checking if file exists
if (file.Exists)
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = ReturnExtension(file.Extension.ToLower());
Response.TransmitFile(file.FullName);
Response.End();
}
システムがファイルの存在を認識していることを確認してください...エラーなしでResponse.End()に到達します...その後、アプリを適切に続行します...ダウンロードプロンプトがないことを除いて。
ReturnExtension メソッドは、次のように別のサイトから持ち上げられます (申し訳ありませんが、場所を思い出せません!)。
string ReturnExtension(string fileExtension)
{
// In the long run this should go in a class
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}
}