RE: Folder Uploads

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Emiliano (anonymous_at_discussions.microsoft.com)
Date: 05/12/04


Date: Wed, 12 May 2004 02:26:04 -0700

No if the code is on the local computer the code is this ^__^

                private string EnsureParentFolder(SPWeb parentSite, string destinUrl)
                {
                        destinUrl = parentSite.GetFile(destinUrl).Url;

                        int index = destinUrl.LastIndexOf("/");
                        string parentFolderUrl = string.Empty;

                        if (index > -1)
                        {
                                parentFolderUrl = destinUrl.Substring(0, index);

                                SPFolder parentFolder = parentSite.GetFolder(parentFolderUrl);

                                if (! parentFolder.Exists)
                                {
                                        SPFolder currentFolder = parentSite.RootFolder;

                                        foreach(string folder in parentFolderUrl.Split('/'))
                                        {
                                                currentFolder = currentFolder.SubFolders.Add(folder);
                                        }
                                }
                        }

                        return parentFolderUrl;
                }

                /*
                 * Funzione : UploadFile
                 * Input : srcUrl : string : url di origine
                 * destUrl : string : url di destinazione
                 * Output : int : 0 se ok
                 * numero eccezione in caso contrario
                 *
                 * Note : inserisce un documento da una macchina locale in un sito
                 */
                public int UploadFile(string srcUrl, string destUrl)
                {

                        string target=@"\";
                        char[] anyOf = target.ToCharArray();
                        int pos = srcUrl.LastIndexOfAny(anyOf)+1;
                        string nome = srcUrl.Substring(pos);

                        if (! File.Exists(srcUrl))
                        {
                                throw new ArgumentException(String.Format("{0} : questo percorso non esiste. Il parametro errato รจ ", srcUrl), "srcUrl");
                        }

                        SPWeb site = new SPSite(destUrl).OpenWeb();
                        site.AllowUnsafeUpdates=true;

                        destUrl+=nome;
                        FileStream fStream = File.OpenRead(srcUrl);
                        byte[] contents = new byte[fStream.Length];
                        fStream.Read(contents, 0, (int)fStream.Length);
                        fStream.Close();

                        EnsureParentFolder(site, destUrl);
                        try
                        {
                                site.Files.Add(destUrl, contents);
                                return 0;
                        }
                        catch(SPException ex)
                        {
                                return ex.ErrorCode;
                        }
                }