none
Error on uploading attached file from Asp.Net MVC 5 Provider Hosted add-in RRS feed

  • Question

  • Hi,

    I am trying to upload attached files to SharePoint document library from Provider Hosted app with Asp.Net MVC 5. I am getting the following error while saving attachments to SP document library-

    An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll but was not handled in user code
    
    Additional information: Could not find file 'c:\windows\system32\inetsrv\Ref2.PNG'.

    I get the error on this line-

     newFile.Content = System.IO.File.ReadAllBytes(fileName);

    in my below method-

    using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(hostWeb.ToString(), appOnlyAccessToken))
                {
                    {                    
                        if (clientContext != null)
                        {
                            foreach (var attachFile in fileUpload)
                            {
                                if (attachFile.ContentLength > 0)
                                {                                
                            spUser = clientContext.Web.CurrentUser;                       
                            string fileName = Path.GetFileName(attachFile.FileName);                       
                            Web web = clientContext.Web;                       
                            List docLibrary = web.Lists.GetByTitle("Documents"); 
                            FileCreationInformation newFile = new FileCreationInformation();                        
                            newFile.Content = System.IO.File.ReadAllBytes(fileName);                    
                            newFile.Url = fileName.ToString();
                            newFile.Overwrite = true;
                            Microsoft.SharePoint.Client.File uploadFile = docLibrary.RootFolder.Files.Add(newFile);
                            clientContext.Load(uploadFile);
                            clientContext.ExecuteQuery();
                            Microsoft.SharePoint.Client.ListItem item = uploadFile.ListItemAllFields;                       
                            string docTitle = string.Empty;
                            item["Title"] = docTitle;
                            item["Keywords"] = "Memo";
                            item.Update();
                            clientContext.ExecuteQuery();                          
                        }
                    }
                }
                   
                    }
                }

    Thanks in advance for any help.

    Monday, October 22, 2018 1:58 PM

Answers

  • Hi,

    Try this:

    if (attachFile.ContentLength>0)
                            {
                                string fileName = Path.GetFileName(attachFile.FileName);
                                List docLibrary = web.Lists.GetByTitle("Documents");
                                FileCreationInformation newFile = new FileCreationInformation();
                                //newFile.Content = System.IO.File.ReadAllBytes(attachFile.FileName);
                                 BinaryReader b = new BinaryReader(attachFile.InputStream);
                                 byte[] bindata = b.ReadBytes((int)attachFile.InputStream.Length);
                                newFile.Content = bindata;
                                newFile.Url = fileName.ToString();
                                newFile.Overwrite = true;
                                Microsoft.SharePoint.Client.File uploadFile = docLibrary.RootFolder.Files.Add(newFile);
                                clientContext.Load(uploadFile);
                                clientContext.ExecuteQuery();
                            }

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by pointtoshare Wednesday, October 24, 2018 7:35 AM
    Wednesday, October 24, 2018 2:45 AM
  • Hi,

    And for MVC Web.config file, not forget to set requestLimits maxAllowedContentLength like this:

    <system.webServer>
       <security>
          <requestFiltering>
             <requestLimits maxAllowedContentLength="1073741824" />
          </requestFiltering>
       </security>
     </system.webServer>
    

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by pointtoshare Wednesday, October 24, 2018 7:34 AM
    Wednesday, October 24, 2018 6:57 AM

All replies

  • Hi

    Seems like the application is looking for a png image file. But why does it exist in system32 folder? I don't think so.

    You may write a relative path in your code and the application is run as admin that located to system folder.


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, October 23, 2018 2:23 AM
  • Hi,

    The Ref2.PNG file is the image attachment I am trying to upload to document library.

    I have tried the following code but get error "Could not find a part of the path "'c:\sites\myteam\Shared Documents\Ref2.PNG'"

    var filePath = docLibrary.RootFolder.ServerRelativeUrl + "/" + fileName;
    newFile.Content = System.IO.File.ReadAllBytes(filePath);

    I have also tried the following code but get error "URI formats are not supported."

    var filePath = docLibrary.RootFolder.ServerRelativeUrl + "/" + fileName;
    var absolutePath = new Uri(clientContext.Url).GetLeftPart(UriPartial.Authority) + filePath;
    newFile.Content = System.IO.File.ReadAllBytes(absolutePath);
    Thanks.

    Tuesday, October 23, 2018 5:36 AM
  • Hi

    If you want to upload file, why did you use a SharePoint relative URL?

    You need to use local system path.


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, October 23, 2018 6:00 AM
  • Hi,

    Could you please help me how do I get the local system path?

    Thanks.

    Tuesday, October 23, 2018 6:25 AM
  • Hi

    I can see from your code that you use a fileUpload coll to enumerate the file.

    Please try to use fileFullPath property when you construct the Stream object.

    Ex. 

    newFile.Content = System.IO.File.ReadAllBytes(attachFile.FullPath);

    Not sure about the exactly property name, debug it and you will find the issue easily.


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, October 23, 2018 6:41 AM
  • Hi,

    I have tried the following but always returns this path "c:\windows\system32\inetsrv\"

    var filepath = Path.GetFullPath(attachFile.FileName);
    newFile.Content = System.IO.File.ReadAllBytes(filepath);

    Thanks.

    Tuesday, October 23, 2018 7:02 AM
  • Hi

    Could you please list attachFile's every property and its value?


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, October 23, 2018 7:12 AM
  • Here are the properties:


    Tuesday, October 23, 2018 7:19 AM
  • Hi

    The FileName is a full path value for local system. Thus,

    1# You needn't to use Path.GetFileName to rebuild the fullpath string, delete this line.

    2# At line newFile.Content, change fileName to attachFile.FileName.

    3# For newFile's URL propery, you will only need to pass the filename but not full path, it is incorrect.

    Please correct them and try again.


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, October 23, 2018 8:00 AM
  • Hi,

    The tried your suggestions but get the same error-

    Could not find file 'c:\windows\system32\inetsrv\Ref2.PNG
    Tuesday, October 23, 2018 8:26 AM
  • Hi pinttoshare,

    This error "Could not find file 'c:\windows\system32\inetsrv\Ref2.PNG'" happened when Path class can't find the specific file in local path.

    In your code fileUpload object is the source of the file which need to be uploaded, so please set a breakpoint in this line:

     foreach (var attachFile in fileUpload)

    Trace fileUpload object, I believe this should be the file collection, check if all the file path is valid.

    The file path "c:\windows\system32\inetsrv\Ref2.PNG". you can open it in Windows Explorer, if the file path is valid, this png can be opened in image viewer.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, October 23, 2018 8:52 AM
  • Hi

    I've done my best to help.

    If you debug your program, you can easily find the cause.


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, October 23, 2018 9:00 AM
  • Hi,

    The Ref2.PNG is not present in this location "c:\windows\system32\inetsrv". The PNG file is being added to the path from the fileName variable, if I try to upload Something.docx the path will be "c:\windows\system32\inetsrv\Something.docx".

    The problem is with the path, in any case the path is "c:\windows\system32\inetsrv".

    Thanks.

    Tuesday, October 23, 2018 9:00 AM
  • Ok, Thanks Justin.
    Tuesday, October 23, 2018 9:06 AM
  • Hi,

    Are you using some Upload control ? Please check its settings. It feels like there is some hard code preappend to the files in the Upload.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, October 23, 2018 9:07 AM
  • Yes, I am using Upload control but don't see anything wrong with this. Thanks.
    Tuesday, October 23, 2018 10:58 AM
  • Hi pointtoshare,

    Could you please share where did fileUpload come from ? This is the key point of this error, all file's name path come from this object.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, October 23, 2018 11:01 AM
  • Here it is -

    <input type="file" id="fileToUpload" name="fileUpload" multiple="multiple" style="float: left;" />

    And, in controller-

    List<HttpPostedFileBase> fileUpload

    Tuesday, October 23, 2018 11:04 AM
  • Here it is -

    <input type="file" id="fileToUpload" name="fileUpload" multiple="multiple" style="float: left;" />

    And, in controller-

    List<HttpPostedFileBase> fileUpload

    Hi,

    Modified the code snippet like below, directly use attachFile.InputStream as file stream and newfile.Url should be SharePoint document library path and the file name:

                    List docLibrary = web.Lists.GetByTitle("Documents");
                    clientContext.Load(docLibrary.RootFolder);
                    clientContext.ExecuteQuery();
                    FileCreationInformation newFile = new FileCreationInformation();
                    newFile.Content=attachFile.InputStream
                    newFile.Url = docLibrary.RootFolder.ServerRelativeUrl.ToString()+"/"+ attachFile.FileName;
                    newFile.Overwrite = true;
                    Microsoft.SharePoint.Client.File uploadFile = docLibrary.RootFolder.Files.Add(newFile);
                    clientContext.Load(uploadFile);
                    clientContext.ExecuteQuery();

    Make sure the InputStream is valid.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


    Tuesday, October 23, 2018 3:06 PM
  • Hi,

    Getting error "Cannot implicitly convert type 'System.IO.Stream' to 'byte[]'" in the below line-

    newFile.Content = attachFile.InputStream;
    Thanks.

    Tuesday, October 23, 2018 5:22 PM
  • Hi pointtoshare,

    Try like this:

    1. View code:

    @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <input type="file" name="fileUpload" multiple="multiple"/>
    
        <input type="submit" value="Upload" name="UploadButton" id="UploadButton" />
    }

    2. Controller Code:

     foreach (var attachFile in fileUpload)
                        {
                            if (attachFile.ContentLength>0)
                            {
                                string fileName = Path.GetFileName(attachFile.FileName);
                                List docLibrary = web.Lists.GetByTitle("Documents");
                                FileCreationInformation newFile = new FileCreationInformation();
                                newFile.Content = System.IO.File.ReadAllBytes(attachFile.FileName);
                                newFile.Url = fileName.ToString();
                                newFile.Overwrite = true;
                                Microsoft.SharePoint.Client.File uploadFile = docLibrary.RootFolder.Files.Add(newFile);
                                clientContext.Load(uploadFile);
                                clientContext.ExecuteQuery();
                            }
                        
                        }

    Still use attachFile.FileName nor fileName in System.IO.File.ReadAllBytes method.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, October 24, 2018 1:44 AM
  • Hi,

    If you see my previous conversation, I already tried it. I always get the source path "c:\windows\system32\inetsrv\" what ever the actual location is. I have no idea why it always find attached file in that location. its occurring on the below line-

    newFile.Content = System.IO.File.ReadAllBytes(attachFile.FileName);

    Thanks,

    Wednesday, October 24, 2018 2:09 AM
  • Hi,

    Try this:

    if (attachFile.ContentLength>0)
                            {
                                string fileName = Path.GetFileName(attachFile.FileName);
                                List docLibrary = web.Lists.GetByTitle("Documents");
                                FileCreationInformation newFile = new FileCreationInformation();
                                //newFile.Content = System.IO.File.ReadAllBytes(attachFile.FileName);
                                 BinaryReader b = new BinaryReader(attachFile.InputStream);
                                 byte[] bindata = b.ReadBytes((int)attachFile.InputStream.Length);
                                newFile.Content = bindata;
                                newFile.Url = fileName.ToString();
                                newFile.Overwrite = true;
                                Microsoft.SharePoint.Client.File uploadFile = docLibrary.RootFolder.Files.Add(newFile);
                                clientContext.Load(uploadFile);
                                clientContext.ExecuteQuery();
                            }

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by pointtoshare Wednesday, October 24, 2018 7:35 AM
    Wednesday, October 24, 2018 2:45 AM
  • Hi Jerry,

    It worked!

    I have also managed it with some modifications- 

    System.IO.Stream fileStream = attachFile.InputStream;
    newFile.ContentStream = fileStream;

    Is it limited to 2 MB max upload? if so then how do I manage more than that size?

    Thanks.

    Wednesday, October 24, 2018 3:44 AM
  • Hi pointtoshare,

    Glad to hear it worked for you. I would suggest you could Mark as answer if it helped.

    And about uploading the file larger than 2MB in the SharePoint add-in, we can consider the last 3 option below:


    Reference and code demo:

    Upload large files sample SharePoint Add-in

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, October 24, 2018 4:50 AM
  • So, the following setup should works for large files right?

    System.IO.Stream fileStream = attachFile.InputStream;
    newFile.ContentStream = fileStream;

    Wednesday, October 24, 2018 6:34 AM
  • Hi pointtoshare,

    Yes, use ContentStream property works for larger file as the third option the capture above specified.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, October 24, 2018 6:50 AM
  • Hi,

    And for MVC Web.config file, not forget to set requestLimits maxAllowedContentLength like this:

    <system.webServer>
       <security>
          <requestFiltering>
             <requestLimits maxAllowedContentLength="1073741824" />
          </requestFiltering>
       </security>
     </system.webServer>
    

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by pointtoshare Wednesday, October 24, 2018 7:34 AM
    Wednesday, October 24, 2018 6:57 AM