質問

私はサーバーで完全に開いている単語文書を持っていますが、ボタンを使ってダウンロードすると、私のウェブサイトのイベントをクリックしますそれは起こります。 以下のコードを使用してボタンをクリックしてドキュメントのダウンロードを行います。 この問題を解決するのに役立ちます。 .NET Framework 3.5

を使用しています
 Response.Clear();
 Response.AddHeader("Content-Disposition", "attachment; filename=StandardLetter" + _responseId.ToString() + ".doc");
 Response.ContentType = "application/octet-stream";

 Response.TransmitFile (Server.MapPath("~/document/letter/StandardLetter" + _responseId.ToString() + ".doc"));
.

役に立ちましたか?

解決

OKここで私が使用するコードは、それはVBですが簡単に変換されます。)

 Response.ContentType = "application/pdf"
        Dim byteArray As Byte() = File.ReadAllBytes(MergedFile)

        Response.AddHeader("Content-Disposition", "attachment;filename=""" & ShortFilename & """")
        Response.AddHeader("Content-Length", byteArray.Length)
        Response.BinaryWrite(byteArray)
        Response.Flush()
        Response.End()
.

PDFでは、.ContentTypeを変更することによって機能します。頑張って!

私はMergedFileというPDF文書を取り出して、それをbyte()に変換する、私はそれをユーザーによって入力できる 'shortname'を与えます。Content-Lengthは非常に重要です..

他のヒント

そのコードの後にResponse.End()がありますか?そうでない場合は、送信されたファイルに追加されたASPXファイルから追加の「HTML」コードを取得します - したがって破損します。

edit
Akshay Anandが言及されているように、HttpContext.Current.ApplicationInstance.CompleteRequest(); docs を参照してください。関連項目この質問

代わりに試してみてください:

Response.ContentType ="application/msword";
.

私は言葉を使わないがExcelのために使います:

Response.ContentType = "application/x-msexcel"
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top