1 if (File1.PostedFile == null)
2 return;
3
4 string destUrl = TextBox1.Text;
5
6 SPWeb site = new SPSite(destUrl).OpenWeb();
7
8 Stream fStream = File1.PostedFile.InputStream;
9 byte[] contents = new byte[fStream.Length];
10
11 fStream.Read(contents, 0, (int)fStream.Length);
12 fStream.Close();
13
14 EnsureParentFolder(site, destUrl);
15
16 site.Files.Add(destUrl, contents);
17
18
19 public string EnsureParentFolder(SPWeb parentSite, string destinUrl)
20 {
21 destinUrl = parentSite.GetFile(destinUrl).Url;
22
23 int index = destinUrl.LastIndexOf("/");
24 string parentFolderUrl = string.Empty;
25
26 if (index > -1)
27 {
28 parentFolderUrl = destinUrl.Substring(0, index);
29
30 SPFolder parentFolder
31 = parentSite.GetFolder(parentFolderUrl);
32
33 if (! parentFolder.Exists)
34 {
35 SPFolder currentFolder = parentSite.RootFolder;
36
37 foreach(string folder in parentFolderUrl.Split('/'))
38 {
39 currentFolder
40 = currentFolder.SubFolders.Add(folder);
41 }
42 }
43 }
44 return parentFolderUrl;
45 }