Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have an ASP.NET application wrapped in SharePoint 2007 website. Within this application I allow users to create requests and they can also upload supporting files for the request. When a user uploads supporting docs of type .docx or .xlsx (the only two that I have found to become corrupt so far) they are becoming 'corrupted' in a sense that when I attempt to open them, I'm prompted with a message saying:

"Excel found unreadable content in 'Book1.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes"

When I click yes, it does repair the file and display the contents correctly, but this is unacceptable to the users. I don't even want them to have the problem.

Here is the code (VB.NET) for my upload click event:

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

Any help would be appreciated. Thanks in advance.

share|improve this question
add comment

1 Answer

It will be the mode you are doing the upload.

Convert the byte array into a stream and you won't get any corruptions. You need to do this by saving the byte array you are creating into a file or something, then open it into a stream. The bug is in .Add() not your code.

Not to hot on my VB.Net at the moment so I can't tell you exactly what to write, but I hope that the fix is straight forward enough.

Summary:

If you use Files.Add corrupts files uploaded as a byte array, so save your byte array as a file and load as a file stream and then use that to add to the Attachment collection.

share|improve this answer
    
I'll give this a shot and let you know how it turns out. Thanks for your help. –  ORION Dec 5 '12 at 18:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.