2

SharePoint2007WebサイトでラップされたASP.NETアプリケーションがあります。このアプリケーション内で、ユーザーがリクエストを作成できるようにし、リクエストのサポートファイルをアップロードすることもできます。ユーザーがタイプ.docxまたは.xlsx(これまでに破損していることがわかった2つだけ)のサポートドキュメントをアップロードすると、それらを開こうとすると、プロンプトが表示されるという意味で「破損」します。メッセージ:

「Excelが「Book1.xlsx」で読み取り不可能なコンテンツを検出しました。このワークブックのコンテンツを復元しますか?このワークブックのソースを信頼できる場合は、[はい]をクリックしてください。」

[はい]をクリックすると、ファイルが修復され、内容が正しく表示されますが、これはユーザーには受け入れられません。私は彼らに問題を起こさせたくありません。

アップロードクリックイベントのコード(VB.NET)は次のとおりです。

Private Sub Upload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Try
    Dim currentUser As SPUser = SPContext.Current.Web.AllUsers("SHAREPOINT\SYSTEM")
    Dim currentUserToken As SPUserToken = currentUser.UserToken
    Using currentSite As SPSite = New SPSite(SPContext.Current.Web.Url, currentUserToken)

        Using CurrentWeb As SPWeb = currentSite.OpenWeb
            CurrentWeb.AllowUnsafeUpdates = True

            Dim attachmentList As SPList = CurrentWeb.Lists("Requests")

            Dim item As SPListItem = attachmentList.GetItemById(CurrentRequestId)

            If FileUpload.PostedFile.ContentLength > Nothing And FileUpload.HasFile Then
                Dim fStream As Stream = FileUpload.PostedFile.InputStream
                Dim contents() As Byte = New Byte(fStream.Length) {}

                fStream.Read(contents, 0, CType(fStream.Length, Integer))
                fStream.Close()
                fStream.Dispose()

                Dim attachments As SPAttachmentCollection = item.Attachments
                Dim fileName As String = Path.GetFileName(FileUpload.PostedFile.FileName)
                attachments.Add(fileName, contents)
                item.Update()
            Else
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "disp_msg", "<script type='text/javascript'>alert('The file contains 0 Bytes of data and will be deleted.'); </script>")
            End If
        End Using
    End Using
    DisplayAttachment(CurrentRequestId)
    btnSave.Focus()
Catch ex As Exception
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "disp_msg", "<script type='text/javascript'>alert('Error Uploading File: " & ex.Message & "'); </script>")
End Try
End Sub

どんな助けでもいただければ幸いです。前もって感謝します。

4

1 に答える 1

4

あなたが持っている問題はこの行にあります:

Dim contents() As Byte = New Byte(fStream.Length) {}

そのはず

Dim contents() As Byte = New Byte(fStream.Length - 1) {}

VB.NET配列は、長さではなく上限を指定します。VB.NETはゼロベースであるため、古い行は最後に1バイト余分に作成されます。

これは通常問題ではありませんが、docxファイルとxlsxファイルはこの余分なバイトに対してより敏感です。

于 2013-03-05T15:10:04.583 に答える