I have a user control to upload files in SharePoint document library

 <asp:FileUpload ID="fileBrowse" runat="server" />
 <asp:Button ID="fileUpload" runat="server" Text="Upload Files" OnClick="fileUpload_Click" />

code behind (Client Object Model):

  protected void fileUpload_Click(object sender, EventArgs e)
        if (fileBrowse.HasFile)
            string filename = fileBrowse.PostedFile.FileName;
            String fileToUpload = @filename;
            String sharePointSite = ".../GxAIM/";
            String documentLibraryName = "Shared Documents";

            using (SPSite oSite = new SPSite(sharePointSite))
                using (SPWeb oWeb = oSite.OpenWeb())
                    if (!System.IO.File.Exists(fileToUpload))
                        throw new FileNotFoundException("File not found.", fileToUpload);

                    SPFolder myLibrary = oWeb.Folders[documentLibraryName];

                    // Prepare to upload
                    Boolean replaceExistingFiles = true;
                    String fileName = System.IO.Path.GetFileName(fileToUpload);
                    FileStream fileStream = File.OpenRead(fileToUpload);

                    // Upload document
                    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

                    // Commit 

when I am on dev machine (remote connection) it works fine, but when I am on local machine with same user credentials, I can upload with the OOTB method on the shared library but if I browse from the user control then I click upload, it loads the documents til 100% then tell me :

[FileNotFoundException: File not found.]

I noticed that In local machine for line :

string filename = fileBrowse.PostedFile.FileName; I have just filename

but in dev machine I have all the path which is correct.

  • Saving the file physically onto server and than working on the same helped me resolve my issue.
    – BKChedlia
    Sep 27 '16 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.