Vulnerability in EPiServer.Forms

Try our conversational search powered by Generative AI!

Upload file to directory in code


I'm trying to upload a file in code to a directory in my File Manager called "Images" but I can't seem to get the directory name right. Any suggestions to what I'm doing wrong? This is what I've got so far but it doesn't seem to find or create a directory name "Images".

protected string UploadFile(Stream fileContent, string fileName)
    UnifiedDirectory dir;

    string virtualDir = "/Diverse/";
    if (HostingEnvironment.VirtualPathProvider.DirectoryExists(virtualDir))
        dir = VirtualPathHandler.Instance.GetDirectory(virtualDir, true) as UnifiedDirectory;
        dir = UnifiedDirectory.CreateDirectory(virtualDir);

    if (dir != null)
        UnifiedFile uFile = dir.CreateFile(fileName);
        Stream s = uFile.Open(FileMode.CreateNew);

        byte[] buffer = new byte[fileContent.Length];
        int numBytesToRead = (int)fileContent.Length;

        fileContent.Read(buffer, 0, numBytesToRead);

        s.Write(buffer, 0, buffer.Length);


        return uFile.VirtualPath;

    else return "";

Sep 29, 2010 13:56

Forgot to change the string virtualDir to ="/Images/". Doesn't make any difference since none of them work... :)

Sep 29, 2010 13:58
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.