简体   繁体   中英

Accessing File Located in Sharepoint List

I'm trying to upload a file from my app web to a sharepoint list, but I'm having some issues when attempting to open/read the file with a filestream.

My code:

    using (var stream = System.IO.File.OpenRead
("https://mysite.sharepoint.com/sites/dev/appweb/document-library/fileFolder/image.png"))
    {
        var folder = ctx.Web.Lists.GetByTitle("Images").RootFolder;

        FileCreationInformation fileInfo = new FileCreationInformation();
        fileInfo.ContentStream = stream;

        stream.Seek(0, SeekOrigin.Begin);
        fileInfo.Overwrite = true;
        fileInfo.Url = "image.png";

        folder.Files.Add(fileInfo);
        ctx.ExecuteQuery();
    }

So I build the uploadFilePath variable using spContext.SPAppWebUrl.ToString(); which assigns the value of the app web to a variable which along with a hardcoded file path and user input for the filename make up my location of the file I want to upload.

The only issue is that when I get to the point of opening my file, using (var stream = System.IO.File.OpenRead("https://mysite.sharepoint.com/sites/dev/appweb/document-library/fileFolder/image.png")) there's an issue due to the semicolon in "https://....." in my app web url being an illegal character.

Is there any way I can access the file without using a full url? I did try trim the "https://" from my url but I think the sharepoint context expects a secure connection and I get an access denied error.

You are using wrong method or argument to open file - check out documentation for File.OpenRead - it opens local files only.

You either need to read stream using HTTP methods (like WebClient/WebRequest) or upload local file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM