locked
giving an error to open .docx and .xlsx files which is uploaded programmatically into sharepoint document library RRS feed

  • Question

  • Please find the below code i am using in my webpart.

    Uploading scenario:
    Protected Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  

    Dim

     

    fileName As String = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName)

     

     

    ' Get a reference to PostedFile object

     

    Dim uploadedFile As HttpPostedFile = fileUpload.PostedFile

     

     

    ' Get size of uploaded file

     

    Dim intFileLen As Integer = uploadedFile.ContentLength

     

     

    ' Allocate a buffer for reading of the file

     

    Dim uploadedFileData(CInt(intFileLen)) As Byte

     

    ' Read uploaded file from the Stream

    uploadedFile.InputStream.Read(uploadedFileData, 0, intFileLen)

     

    Dim Site As New SPSite(ConfigurationManager.AppSettings("SPSiteName").ToString())

     

     

    Dim Web As SPWeb = Site.OpenWeb()

    Site.AllowUnsafeUpdates =

     

    True

    Web.AllowUnsafeUpdates =

    True

     

    Dim list As SPList = Web.Lists(ConfigurationManager.AppSettings("DocRepository").ToString())

     

     

    Dim oListItemCollection As SPListItemCollection = list.Items

     

     

    Dim DestinationFolder As SPFolder = Web.GetFolder(ConfigurationManager.AppSettings("DocRepository").ToString())

     

    Dim strFileNameWithOutExtension As String = System.IO.Path.GetFileNameWithoutExtension(fileUpload.PostedFile.FileName)

     

     

    Dim strFileNameExtension As String = System.IO.Path.GetExtension(fileUpload.PostedFile.FileName)

    fileName = strFileNameWithOutExtension &

     

    "_" & String.Format("{0:MMddyyyy_HHmmss}", GetCycleTestingDate()) & strFileNameExtension

    DestinationFolder.Files.Add(fileName, uploadedFileData)

     


    End

     

     

    Sub


    View Document scenario:
    binding
    http://testsvr:15824//DocLib/testdocument_08142009_033059.docx

    <

     

    asp:HyperLink Target="window" ID="lnkbtn" runat="server" NavigateUrl='<%#DataBinder.Eval(Container.DataItem, "URL")%>'

     

     

    when click on the document name from webpart, displaying error message is

    "The Office Open XML file testdocument_08142009_033059.docx cannot be opened because there are problems with the contents."

    when click on "OK" then displaying

    "Word found unreadable content in testdocument_08142009_033059.docx. Do you want to recover the contents of this document? If you trust the source of this document, Click Yes."

    But i am able open the .doc and .xls and .pdf files. Only Problem with .docx , .xlsx files.

    Can any one help me?

     

     

     

    Friday, August 14, 2009 11:35 AM

Answers

  • Hi,

    Next question, if you upload a .docx, .xlsx to the same doc. lib. manually through the web user interface, can you open that from the library?

    Intead of reading the stream to the byte array, you can try to use the stream (fileUpload.PostedFile.InputStream) using the Add method (String, Stream), http://msdn.microsoft.com/en-us/library/ms414180.aspx.

    Since you don't want to modify the content using the byte array, it is not required to read the content there, and it is extra work. Might not help, but you can give it a chance.

    Peter
    Friday, August 14, 2009 11:56 AM

All replies

  • Hi,

    I assume you can open the same (original) .docx, .xlsx files using Word, Excel. Am I right?

    Peter

    Friday, August 14, 2009 11:47 AM
  • Yes. But those files uploaded into sharepoint library through code. And displaying the file names in custom webpart. binding the file URL to the hyperlink button.
    Friday, August 14, 2009 11:56 AM
  • Hi,

    Next question, if you upload a .docx, .xlsx to the same doc. lib. manually through the web user interface, can you open that from the library?

    Intead of reading the stream to the byte array, you can try to use the stream (fileUpload.PostedFile.InputStream) using the Add method (String, Stream), http://msdn.microsoft.com/en-us/library/ms414180.aspx.

    Since you don't want to modify the content using the byte array, it is not required to read the content there, and it is extra work. Might not help, but you can give it a chance.

    Peter
    Friday, August 14, 2009 11:56 AM
  • Hi Peter,

    Using Stream method, it is working fine.

    Thank you for your help.

    Dim

     

    strm As System.IO.Stream = fileUpload.PostedFile.InputStream

    Dim

     

    list As SPList = Web.Lists(ConfigurationManager.AppSettings("DocRepository").ToString())

    Dim

     

    collFilesDest As SPFileCollection = list.RootFolder.Files

    Dim

     

     oFiles As SPFile = collFilesDest.Add(fileName, strm)

    Raju

    Friday, August 14, 2009 12:34 PM
  • Hi Raju,

    A little bit strange. But the main point, that it works now.

    If my reply was helpful or answered your question, please mark it as such. If you need more help on the topic, please, let us know how we can help you!

    Thanks!

    Peter

    Friday, August 14, 2009 12:42 PM