0

I have a custom list and i'm using the below.But getting error:

Could not find file 'c:\windows\system32\inetsrv/Filename -System.IO.FileNotFoundException

 if (FileUpload1.HasFile)
        {
            var attachment = new AttachmentCreationInformation();
            //attachment.FileName = @"C:\Others\Exmec\" + FileUpload1.FileName;
            attachment.FileName = FileUpload1.PostedFile.FileName;


            attachment.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(FileUpload1.PostedFile.FileName));
            Attachment att = oListItem.AttachmentFiles.Add(attachment);
            ctx.Load(att);
        }

Any ideas what i'm doing wrong?

Thanks in Advance

  • Is any of the answer helps you? – Hardik May 13 '16 at 12:47
0

Below code is working fine for me. Yours is same except I see execute query command to be missing. Also check if the location specified in the error is accessible and correct parameters are being passed in FileName and ReadAllBytes().

 var attInfo = new AttachmentCreationInformation();
attInfo.FileName = mFile.Name;
attInfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes("streamFile"));

Attachment att = item.AttachmentFiles.Add(attInfo); //Add to File

context.Load(att);
context.ExecuteQuery();
| improve this answer | |
  • SHould you not close the stream as well? – Robert Lindgren May 13 '16 at 9:02
  • Not required... The issue is the location issue..If user points it to other destination the code will work. – Ankit Kumar May 13 '16 at 9:11
  • Thanks for the reply. I do have the ExecuteQuery() in my code just didn't include it. What needs to happen is when a user is using the form he/she should be able to browse the file from any drive or location to upload. Why is it trying to find the files in c:\windows\system etc... – naijacoder May 16 '16 at 1:20
0
using (ClientContext context = new ClientContext("http://SharepointURL/"))
{
    try
    {


        Web web = context.Web;

        FileCreationInformation newFile = new FileCreationInformation();
        newFile.Content = System.IO.File.ReadAllBytes(@"C:\test.txt");
        newFile.Url = "test.txt";

        List docs = web.Lists.GetByTitle("CustomList");
    ListItem item = docs.GetItemById(ID);

    var attInfo = new AttachmentCreationInformation();
    attInfo.FileName = mFile.Name;
    attInfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes("streamFile"));

    Attachment att = item.AttachmentFiles.Add(attInfo); //Add to File

    context.Load(att);
    context.ExecuteQuery();
    Console.WriteLine("done");


    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Hope this code helps you to attach file to list item.

If you find any problem in it than please let me know.

| improve this answer | |
  • Thanks guys what i found that is that my code works in IE but not in firefox. When i use firefox i can't seem to get the full path of the file and that's why its failing. Any suggestions or workarounds? – naijacoder May 16 '16 at 2:24
  • You should use Server.MapPath(FileUpload1.FileName); to get the file path from where is it going to get the file... – Hardik May 16 '16 at 2:46
  • Thanks Hardik . The server MapPath did work in Firefox but not in IE .. :) – naijacoder May 16 '16 at 6:36
  • Sorry this is the snippet I'm using: attachment.FileName = Server.MapPath(FileUpload1.FileName); attachment.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(Server.MapPath(FileUpload1.PostedFile.FileName))); – naijacoder May 16 '16 at 7:18
  • In IE i get "is a physical path, but a virtual path was expected. " – naijacoder May 16 '16 at 7:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.