Try our conversational search powered by Generative AI!

Upload file to directory in code

Vote:
 

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;
    else
        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);
        s.Close();

        fileContent.Close();

        return uFile.VirtualPath;
    }

    else return "";
}

#44029
Sep 29, 2010 13:56
Vote:
 

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

#44030
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.