Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 7589

Trending Articles