1

私のウェブページにはボタンがあります。それをクリックすると、ブラウザにドキュメントを送信したいと思います。

クリックイベントのコードは次のとおりです。

Private Sub btMNCgetTemplate_Click(sender As Object, e As EventArgs) Handles btMNCgetTemplate.Click
    Dim MNCid As Integer = Me.cbMNCrequestType.SelectedValue
    Dim mncRT As New MinorNetworkChangeTypeOfRequests
    Dim MNCrq As New MNCTypeOfRequestItem

    MNCrq = mncRT.Find(MNCid)
    If MNCrq IsNot Nothing Then
        If MNCrq.Form.ToLower.EndsWith(".doc") Or 
           MNCrq.Form.ToLower.EndsWith(".docx") Then
            Response.ContentType = "Application/msword"
        Else
            Response.ContentType = "Application/x-msexcel"
        End If

        Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", MNCrq.Form))
        Response.TransmitFile(Server.MapPath(String.Format("~/forms/{0}", MNCrq.Form)))
        Response.End()
    End If
End Sub

MNCrqオブジェクトのFormプロパティには、ファイルの名前があります。

最初はこれは問題なく機能し、ユーザーはファイルの保存ウィンドウを取得しました。しかし、今ではもう機能していません。Chromeでウェブサイトを実行しても何も起こりません。IE9でWebサイトを実行すると、自分のものではないファイルに次のエラーメッセージが表示されます。

Unhandled exception at line 940, column 13 in http://localhost:29226/ScriptResource.axd?
d=DbqlGCg_y1TWNdNykQXSWTqf7VMHZvfOOc8W9SvKy5VJEvrKhkNOK5JNcaIC4d76X42JcWSxljh5epK1GqlRC4_NnfoLlKD1PfZ2-dNg98DHOKlBmICo8PKGlg73PqEQJR5AdM_sf6udu_6Vkp3cg9MicDI1&t=7c776dc1

0x800a139e - Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

私はここで何が間違っているのですか?

rg、エリック

4

3 に答える 3

1

使用できますWriteFileServer.MapPath(String.Format("~/forms/{0}", MNCrq.Form))存在する有効なファイルを返すようにしてください。

Response.AddHeader("Content-Disposition", 
   String.Format("attachment; filename={0}", MNCrq.Form))
Response.WriteFile(Server.MapPath(String.Format("~/forms/{0}", MNCrq.Form)))
Response.End();
于 2013-03-20T15:05:13.487 に答える
0

私はほとんどプロではありませんが、一度自分でやったことを提案しようと思います:)

あなたのウェブサイトのディレクトリにファイルを追加して、ランダムな例として、

Server.Transfer("C:\Users\Eric\Desktop\Website\Attachments\WordFile.docx") 

または、ボタンをハイパーリンクに変更してファイルにリンクしますか?これは、物事を単純化する方法であり、必要なコードが少なくて済み、エラーが発生しにくくなります。

于 2013-03-20T14:48:01.710 に答える
0

別のソリューションを使用しました。

新しい ASPX ページを作成し、load イベントに次のコードを追加しました。

 Dim bestand As String = Page.Request("file")

Response.ClearContent()
Response.ClearHeaders()
Dim fi As New FileInfo(Server.MapPath(".\forms\") + bestand)

Response.ContentType = "application/x-unknown" ' arbitrary 
Response.AddHeader("Content-Disposition", "attachment; filename=" + bestand)
Response.AddHeader("Content-Length", fi.Length.ToString())

Response.BinaryWrite(File.ReadAllBytes(fi.FullName))

Response.End()

ボタンイベントから、次のように呼び出します。

 Response.Redirect(String.Format("givefile.aspx?file={0}", MNCrq.Form), False)

今、私はファイルを取得します。

rg。エリック

于 2013-03-20T15:16:54.477 に答える