Word cannot open file when uploaded document to document library progrmmatically.

I have created one button in which I am uploading a new document to document library through my code. The document library contains document template.
My code is as below:
private SPListItem CreateDocument(SPList Docs, string title)
SPListItem newItem = null;
try
SPFolder cmsDocsFolder = Docs.RootFolder;
SPFileCollection fileCollection = cmsDocsFolder.Files;
if (!string.IsNullOrEmpty(Docs.ContentTypes[0].DocumentTemplateUrl))
string docTemplateUrl = Docs.ContentTypes[0].DocumentTemplateUrl;
SPFile docTemplateFile = Docs.ParentWeb.GetFile(docTemplateUrl);
byte[] binaryFile = docTemplateFile.OpenBinary();
string newFileUrl = cmsDocsFolder.Url + "/" + RemoveSpecialCharacters(title).Trim() + _docxExt;
if (!Docs.ParentWeb.GetFile(newFileUrl).Exists)
SPContentType contentType= Docs.ContentTypes.Cast<SPContentType>().FirstOrDefault(c => c.Id.IsChildOf(ContentTypeIDs.CMSDocument));
if (contentType != null)
SPFile addedFile = fileCollection.Add(newFileUrl, binaryFile, false);
newItem = addedFile.Item;
newItem[FieldNames.ContentTypeId] = ContentTypeIDs.CMSDocument;
newItem[FieldNames.ContentType] = contentType.Name;
catch (Exception ex)
return newItem;
My code does not throw any error but when I go to document library and click on uploaded document the file tries to open in word application but I am getting an error:
"Word cannot open the file because the file format does not match the file extension. (url)"
The browser I am using is FF.
What am I missing? Please help.

Hi,
Please try to upload your document using SharePoint UI.
If the issue still exists in Firefox, I suggest you check your Firefox settings.
If not, please debug your code and watch the value or here is a blog for your reference:
http://thearkfloats.blogspot.com/2013/10/programmatically-upload-to-sharepoint.html
Best Regards
Dennis Guo
TechNet Community Support

Similar Messages

  • CS5 InDesign cannot open files in the InDesign CS5 document format

    I'll be working in InDesign CS5 with no problems then at some point I'll double click on an InDesign file and a window comes up and says - InDesign cannot open files in the InDesign CS5 document format. Sometimes when this happens I have to completely reboot my whole system before I can get InDesign to function correctly. Is this a flaw with CS5? Did not happen with previous versions. It doesn't always happen but I can't figure out what causes it. Would appreciate anyones help. I'm getting to really HATE InDesign!!

    I have not uninstalled and reinstalled, but I did just switch over to another workstation using Time Machine. The previous system was Leopard. This one is running Snow Leopard.
    I'm not sure how to parse this. When you say "switch over to another workstaiton using Time Machine" do you mean, "started using another machine that happens to be using Time Machine for backups and is also running 10.6 instead of 10.5"?
    Or do you mean, "Used the Time Machine program to upgrade from 10.5 to 10.6?" If you mean the second one, well, I can't say I am surprised that things are messed up. And that would make your problem rather different than the other posters, which is good reason for you to have started your own thread (and thus your own solution)....
    I'm a little bit leary of getting in and changing things in Terminal, without understanding what I am changing. Plus this is a workstation at work, so if I screw something up, that is not a good things, and I don't have time to spend a day fixing things back up again, with the way our workload is.
    Well, you can find a lot of marginal documentation about lsregister and Launch Services, but I wonder whether your understood the ramifications of messing with Time Machine (or maybe I have misinterpretted you!). I'd be surprised if you found another way to fix this, other than uninstall/reinstall.
    Good luck.

  • Illustrator cannot open files in the "Adobe Illustrator Document" format.

    I recently upgraded from CS4 to CS5.5 on my Mac (10.6.7), and now when I try to open a .ai file, I get the error:
    Illustrator cannot open files in the “Adobe Illustrator Document” format.
    Google doesn't seem to be much help other than one other person who had the same issue. Any idea why Illustrator can't open Illustrator documents?

    Actually, quitting Illustrator and re-launching it solved it. It was the first time I had launched Illustrator after installing it, so maybe it had something to do with that.

  • Acrobat cannot open files in the "Adobe PDF document" format

    The document “example.pdf” could not be opened. Acrobat cannot open files in the “Adobe PDF document” format.
    Hi All,
    Running Acrobat Pro 9.4.2 on Snow Leopard, have never had this problem before. Showed up about a week ago, I believe with new update. Can't double click on a file in Finder, or drag and drop to AcrobatPro in Dock, but can open from within Acrobat Pro / File / Open command.
    All my preferences are correct with "Open With Acrobat Pro" as the default.
    I've read all the threads I can find with no answers yet.
    Anyone found and answer, I see a lot of us are going through this over the last year.
    Only possible common thread I have found is possibly HP printers. But I've been running HP printers all along with no issues.
    Thanks for you input.

    Moved to Acrobat Installation & Update Issues
    Either Acrobat needs to be re-installed or the file is named as a pdf file, but it is not a pdf file. Try opening the pdf file in a text editor and read off what the first line of the file is.

  • I keep on getting the message 'AppleScript Editor cannot open files in the "AppleScript Editor Document" format' when I try download software?

    Is there a way to still download these programs?

    Firefox reports that it is my default browser. I changed the default browser to Chrome and then back to Firefox, in case that's what you meant by "redo." Right-clicking and picking Firefox in the Open With menu works, but double-clicking the file or just picking Open in the right-click menu does not.
    I don't know why this is. Could it be due to a version update? I've noticed that Google Notifier's "Go to Inbox" no longer opens up Gmail in Firefox either.

  • Word cannot open this document.  The document might be in use, the document might not be a valid Word document,  or the file name might contain invalid characters (for example,\ /).

    I recently upgraded to a new Macbook Air running Lion, from my 2007 white Macbook running Snow Leopard.  I had Office for Mac 2004 on the 2007, but had to upgrade to Office for Mac 2011 to be compatible with Lion on the Air.  Now when I try to open my documents on the Air that were saved under Office 2004 on the old computer, I get the following message: Word cannot open this document.  The document might be in use, the document might not be a valid Word document,  or the file name might contain invalid characters (for example,\ /).
    Any suggestions?  Thank you.

    Ha,ha,ha.....I figured it out.  But for anyone else who's having this problem, it seems that while Office 2004 allowed dates in this format: 00/00/00, Office 2011 doesn't like it at all.  I just changed the format in the document names to 00-00-00 and all is well.  Documents are opening beautifully! 

  • HT3350 what can you do when you get this warning? "Scouting Book  Collection Index" could not be handled because Numbers cannot open files in the "Numbers Document" format.

    “Scouting Book  Collection Index” could not be handled because Numbers cannot open files in the “Numbers Document” format.

    This file was created in 2002 in MSword but this had been forgotten.
    When I went to open it.... (MSword) was not a program option on my laptop. For some reason, I assumed that either Pages or Numbers wouild open the file. How the file was created was forgotten and had no file Info, but I new that it was a table of valuable information.
    The warning threw me off, because it led me to believe that it was a Numbers file. which Numbers would not open. A paradox.
    I took the file to a laptop in OSXpanther with MSoffice and it opened in MSword. Problem Solved.
    Thank you for your help.

  • Mac Word 2008 - hyperlink message "Word cannot open the specified file"

    I'm using WordMac 2008 version 12.2.5 and after following detailed instructions on how to hyperlink chapters in a Table of Contents to their respective chapters in a book, I can only get the message "Word cannot open the specified file".
    Any ideas what could be wrong?
    My OSX is 10.5.1.
    The instructions I followed are pasted below and are copied from the Smashwords Style Guide: http://www.smashwords.com/books/view/52
    START OF PASTE FROM SMASHWORDS STYLE GUIDE:
    How to create a linked Table of Contents. Consider this feature in “beta.” My
    thanks to Smashwords author Cheryl Anne Gardner for sharing these tips below,
    which I have augmented based on my own experience adding a ToC to the Style
    Guide (woo hoo!).
    By adding a linked table of contents, you can help readers navigate your
    document. This is especially useful for non-fiction. Most fiction doesn’t
    need it. If you follow the instructions below, the ToC should work in your
    EPUB, MOBI and PDF files, but it will not work in our other formats.
    First, create your table of contents where you want it by typing it out. Make
    sure the text is normalized just like the rest of your Smashwords’ Word
    document. Example:
    Contents
    Chapter 1
    Chapter 2
    Chapter 3
    Chapter 4
    Etc.
    Next, we are going to create the BookMarks. In Word 2000, 2003 and 2007, the
    Booksmark function is under the Insert tab. Go through your document, and at
    each Chapter Start or Indexed item, you will highlight it and then select
    Insert: bookmark. Name your bookmark corresponding to the Chapter/Indexed
    item and then click add. It’s very important to use the first word of your
    heading you’re linking to, because later on, when you add the hyperlinks,
    Word displays the bookmark shortcuts in alphabetical order. With many
    bookmarks, I found many of my bookmarks weren’t labeled properly, so it was
    tough to link to the correct bookmark. If you want to link to a chapter named
    How to Publish Ebooks, I’d recommend naming your bookmark
    “HowToPublishEbooks” to make it easy to locate. If you instead name it,
    “publishebooks,” you’ll frazzle your brain and make mistakes.
    Repeat the process above for every Chapter Start as well as for other matter
    in the book like index, bibliography, epilogue, prologue, etc. I didn’t link
    to every section in the Style Guide, because otherwise the ToC would have
    been three pages long. This is your call.
    Next, return to your table of contents and highlight the word “Contents.”
    Click insert, bookmark, and label this last bookmark “Contents”. Formatting
    note: Bookmarks cannot have spaces or odd characters. If instead of
    “Contents” you called the label in the document Table of Contents, as I did,
    then when you insert the bookmark, name the bookmark “TableofContents,” one
    word, or “toc.”
    Next, add the hyperlinks to your Chapter Starts. In your table of contents,
    if Chapter 1 is called Chapter 1, then highlight Chapter 1 and then click
    insert Hyperlink. As you can see in the Style Guide, I tried to use more
    descriptive labels. In Word’s Hyperlink menu on the left hand side, select
    “Place in this document.” You will now see your bookmarks listed in the main
    window. Select Chapter1 bookmark (or whatever you labeled it) and then click
    ok. Do this procedure for every chapter and piece of matter in the table of
    contents.
    Final step. Now you can link back to the Table of contents. Move through your
    document to each indexed item or Chapter start and highlight it. Then click
    insert, hyperlink, and then select “Place in this document.” When the
    bookmarks are showing in the main window, select the bookmark labeled
    “Contents,” assuming that’s what you named your Table of Contents. Do this
    for each and every chapter and or indexed item.
    After you are done, all your links will be underlined. You will need to test
    and make sure you linked everything properly. In most Word versions, you just
    click on the hyperlink to test. In Word 2007, you may need to Ctrl-Click each
    hyperlink and make sure it takes you where you need to go in the document.
    Advice: Word has an automatic ToC generation feature under the reference tab.
    Do not use this. It uses automated headings and formatting to generate the
    ToC and this will un-normalize your text and cause font conversion issues in
    your Smashwords files.
    END OF PASTE FROM SMASHWORDS STYLE GUIDE.
    I did all that as described but I still get the message "Word cannot open the specified file".
    Could you help please?

    This question would be better addressed to...
    http://www.microsoft.com/mac/help.mspx
    I think the operative phrase from that article is...
    *Consider this feature in “beta.”*
    -mj

  • XML Report output giving error "word cannot open the file because....

    Hi All,
    Greetings!
    I have a standard report, 'Payables Posted Invoice Register".
    The report was going in to warning beacuse of some Invalid characters in the Data.
    I corrected the invalid characters and then ran the report ; it completed fine.
    But now when I try to open the report output, the output is not getting open but its allowing me to save the file.
    When I save it on my local machine and try to open it, I am getting an error as "word cannot open the file because it is larger than 512 megabytes".
    The document size showing is 664MB when I saved it on my machine. And the output type of the report is RTF and not PDF.
    Can anybody please help?
    Thanks,
    Sachin

    not sure, if the Microsoft word has limitation of file size.

  • INDD CS6 error: "InDesign cannot open files in the "InDesign CS5.5 Document" format.

    Working in CS6 on Mac OS X 10.7.5: When I double click an INDD file in the Finder, I get the following error from InDesign:
    "The document "doc name" could not be opened. InDesign cannot open files in the "InDesign CS5.5 Document" format." 
    This just started recently and I have not been able to figure out why. I have not updated INDD or OS X. I can't think of anything else that changed, other than maybe a Java update. Have not updated to the latest OS yet. 
    The files I'm trying to open are files I created with CS6 - as recent as a few minutes ago.  I can open them fine using the File>Open command in InDesign so I can open them, but I can't open them by double clicking them in Finder or dragging them to the INDD icon in the dock.
    The info in Finder's "Get Info" window says they are CS6 files set to open in CS6 and I have permissions to read and write. They are not locked.  
    This is not incapacitating, but rather annoying and slows me down since this is one of the most common ways I open files. 
    Any suggestsion would be appreciated.

    Probably won't work because it sounds like an OS problem of some sort, but you can try trashing the prefs. See Replace Your Preferences

  • How to fix file when it says, "Cannot open file " ". Adobe Indesign does not support the file format, a plug-in that supports the file may be missing, or file may be open in another application." It worked one second, I saved and closed the file, then tri

    How to fix file when it says, "Cannot open file " ". Adobe Indesign does not support the file format, a plug-in that supports the file may be missing, or file may be open in another application." It worked one second, I saved and closed the file, then tried to reopen and now it won't work. I am using Adobe InDesign CS5. It is the only version on my comp. I have tried opening on another comp & copying the file – same error comes up. PLEASE HELP

    First check the version of the document with Jongware's script "Identify.jsx" (ExtendScript/JavaScript).
    You can find it here:
    [Ann] Identify Your InDesign File
    If it is CS 5.5 or above, you need someone to open it in the version the script says and export an IDML representation from that. Another way would be to install a 30days version of CS6 or above (CC) and do it yourself.
    In regards of the script showing a result for InDesign files higher than CS6:
    CS7 = CC v9
    CS8 = CC v10 = CC-2014 or CC2014.1
    Uwe

  • My FaceTime seemed not working and keep  telling me The document "IMG_3054.JPG" could not be opened. FaceTime cannot open files in the "JPEG image" format.  I clicked OK but it pop-up again.  So that I can't turn off my computer at all.

    When I tried to restart my computer, FaceTime will pop-up and a warning message The document “IMG_3054.JPG” could not be opened. FaceTime cannot open files in the “JPEG image” format.  I tried to clike OK but this worning message keeping pop-up and I can not quit FaceTime.  So that I can't turnoff or restart my computer.
    Can someone help?

    The next time that happens, try this keyboard shortcut.
    Command + Option + Esc
    That way you can "force quit" the Facetime App.

  • I cannot open files attached to an excel spreadsheet. I am using office for mac 2011. when double clicking object I only get the picture modifier windows.

    I cannot open files attached to an excel spreadsheet. I am using office for mac 2011. when double clicking object I only get the picture modifier windows.

    Preventing Microsoft Office to open recent documents at startup in Mac OS, Please see:
    http://www.frenchguys.com/wordpress/preventing-microsoft-office-open-documents-startup-mac-os-lion/
    It seems that the solution works for most people.
    In a terminal, enter the following:
     defaults write com.microsoft.Excel NSQuitAlwaysKeepsWindows -bool false

  • Word 2008 "Word Cannot Open this Document"

    I couldn't find a better forum so I'll put this here.
    I have Microsoft Word Version 12.0, and have had it installed for a few months. I set the saving format to compatable by default (so it makes .doc and not .docx). Yesterday I was working on a word file I had saved on my thumb drive and it worked fine. Today I try to open it (both on my Macbook, my iMac and on a PC I had access to) and it gives me the following error: "Word cannot open this document. The document might be in use or might not be a valid Word document". I have noticed that this has happened to half a dozen of my other word files also. I notice that on my Macbook all of the word files have the new plain black and white icons that show the text in tiny writing, while the ones I can't open have the old blue file icons (on other, older computers they seem the same).
    So I have three questions:
    Is this a problem others have faced? If so, how can I get around it?
    Are those particular files just corrupted? If so, is this a problem I ought to be wary of with Word 12?
    If they are corrupted, is there any way I can rescue these texts? It's a very important document.
    I eagerly await your responses.

    Have you tried the Microsoft Word for macs forums? http://www.officeformac.com/ProductForums/

  • I am suddenly unable to open my pictures in Adobe Photoshop. It gives the following message: The document "Tina-19.jpg" could not be opened. Photoshop cannot open files in the  "Adobe Photoshop JPEG file" format. What does this mean? I have opened my phot

    I am suddenly unable to open my pictures in Adobe Photoshop. It gives the following message: The document “Tina-19.jpg” could not be opened. Photoshop cannot open files in the  “Adobe Photoshop JPEG file” format.
      What does this mean? I have opened my photos in Adobe Photoshop for years and suddenly I get this message and can't open any of my pictures

    JoeLib wrote:
    You type way faster than me Gene.
    Well, not necessarily in this case.
    According to the time stamp, Gene posted his answer a bit over three hours before you did, Joe.
    Gene's post:       Dec 29, 2014 5:54 PM
    Joe's post:          Dec 29, 2014 8:57 PM

Maybe you are looking for