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.