ユーザーがリンクをクリックしてファイルをダウンロードできるように、HyperLink コントロールのNavigateUrlパスをファイルに設定しようとしています。私の C# および ASPX コードでは、URL が適切に設定されているように見えますが、リンクをクリックしても何も起こりません。リンクを右クリックしてリンクを保存しようとすると、download.htm.
舞台裏では、一時ディレクトリにファイルを作成し、ダウンロード可能な場所に移動しています。この部分は機能します。参考までに載せておきます。
    private string MoveFile(string file_name, string file_path)
    {
        string tempdir = HttpContext.Current.Server.MapPath( ConfigurationManager.AppSettings["docs_folder"] );
        string new_file_name = Path.Combine( tempdir, Path.GetFileName( file_name ) );
        try
        {
            if (File.Exists( new_file_name ))
            {
                File.Delete( new_file_name );
            }
            File.Move( file_name, new_file_name );
        }
        catch (Exception e)
        {
            string message = e.Message;
        }
        return new_file_name;
    }
そして、これが私が現在URLを設定している方法です:
        string download_file = downloader.DownloadFiles(); //Ultimately returns the path from the MoveFile method
        hl_download.NavigateUrl = "file:///" + HttpUtility.UrlPathEncode( download_file ).Replace("\\","//");
        hl_download.Visible = true;
私の出力は次のようになります。
file:///C://users//codes%20with%20hammer//documents//visual%20studio%202012//Projects//Download%20File//Download%20File//Archive_to_download.zip
この URL をブラウザに直接ドロップすると、ファイルが正しくダウンロードされます。では、ハイパーリンクで機能しないのはなぜですか?
また、静的ハイパーリンクにプロパティを設定しようとしました。同じ結果です。比較のために:
<asp:HyperLink ID="HyperLink1" runat="server" Visible="True" NavigateUrl="file:///C:/users/codes%20with%20hammer/documents/visual%20studio%202012/Projects/Download%20File//Download%20File//Archive_to_download.zip">click me</asp:HyperLink>
改訂
私の ASHX ページには、次のコードが含まれていますProcessRequest。
        context.Response.Clear();
        context.Response.ContentType = "application/octet-stream";
        context.Response.AddHeader( "Content-Disposition", "attachment; filename=" + Path.GetFileName(  download_file ));
        context.Response.WriteFile( context.Server.MapPath( download_file ) );
        context.Response.End();
おそらく、これをコード ビハインド ファイルに移動する必要がありますか?