Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Programatically uploading a document to a document library....

70 views
Skip to first unread message

Pai

unread,
Aug 21, 2003, 6:20:48 PM8/21/03
to
Hello there,
I have a textbox whcih holds a value "C:\test.txt"
I want to upload this file to the document library.
I am using the following code:
1. string strConcept_Proposal = "Concept Proposals";
2. SPSite my_Site = SPControl.GetContextSite(Context);
3. SPWebCollection my_WebCollection = my_Site.AllWebs;
4. foreach (SPWeb my_Web in my_WebCollection)
{

5. SPListCollection my_ListCollection = my_Web.Lists;
6. my_ListCollection.IncludeRootFolder = true;
7. foreach(SPList my_list in my_ListCollection)
8. {
9. if (my_list.BaseType == SPBaseType.DocumentLibrary)
10. {
11. SPDocumentLibrary my_DocLibrary = (SPDocumentLibrary)my_list;
12. doc_title = my_DocLibrary.Title;

13. if ( doc_title.CompareTo(strConcept_Proposal) == 0)
14. {


15. SPFolder spfolder = my_Web.Folders["Concept Proposals"];
16. string dest_url = my_Web.Url + "/Concept%20Proposals/";

17. byte[] binFile = my_test_file.OpenBinary();
18. SPFolder spfolder = my_Web.Folders["Concept Proposals"];
19. SPFile spfile = spfolder.Files.Add(dest_url,binFile);

20. SPListItem splistitem = spfile.Item;
21. splistitem.Update();
22. }

23. }
24. }

25. }
26. }

SPFolder's Add method's 2nd parameter is of type "byte[] file".
I am stuck as to how to fit this in my code...
Any Suggestions how to go about this...
Please help
Kind Regards,
Pai

James Edelen [MVP]

unread,
Aug 21, 2003, 8:19:46 PM8/21/03
to
What exactly is wrong with the current code. It looks OK to me (at least
the file upload method does. I didn't look too much at the other code).
Here is how i usually do it though:

System.IO.FileInfo file = //create or define file info object

System.IO.FileStream stream = file.Open(System.IO.FileMode.Open);
System.IO.BinaryReader myReader = new BinaryReader (stream);

SPFolder myFolder = //define SPFolder

myFolder.Files.Add(file.Name, myReader.ReadBytes((int)(stream.Length)),
myWeb.CurrentUser, myWeb.CurrentUser, file.CreationTime,
file.LastWriteTime);


--
James Edelen
Microsoft MVP - SharePoint Portal Server
Microsoft MVP - Windows XP Media Center
Microsoft Associate Expert
Expert Zone - http://www.microsoft.com/windowsxp/expertzone
"Pai" <sp...@immunetolerance.org> wrote in message
news:5332f465.0308...@posting.google.com...

Michael Linster

unread,
Sep 8, 2003, 12:49:55 PM9/8/03
to
I have been working on doing the import as well, and have another
error message that is coming up. I am trying to add a file to a
document library not in the root and get an access denied error
message when I am using this code:

SPFile libraryFile = destinationFolder.Files.Add(destinationFolder.Url
+ "/" + Path.GetFileName(strFilePath), fileData,
currentWebArea.CurrentUser, currentWebArea.CurrentUser, dateCreated,
dateModified);

If I use this code it works fine:
SPFile libraryFile = destinationFolder.Files.Add(destinationFolder.Url
+ "/" + Path.GetFileName(strFilePath), fileData, true);

Anyone know why? It works fine if we copy the files to the root
document library or a WSS site, but not a document library not in the
root.

Any help would be appreciated.

Mike

"James Edelen [MVP]" <j...@mvps.org> wrote in message news:<uDsaaMEa...@TK2MSFTNGP10.phx.gbl>...

James Edelen [MVP]

unread,
Sep 8, 2003, 2:11:20 PM9/8/03
to
Since the DL is not off the root, it must be off a web. I would create a
instance of that web rather than using the root web.

For example: if you root was http://localhost and your document library was
under http://localhost/marketing, you would do something like this

SPSite site = new SPSite(http://localhost);
SPWeb web = site.AllWebs["Marketing"]; //where Marketing is the name of the
site

then run my original code.

--
James Edelen
Microsoft MVP - SharePoint Portal Server
Microsoft MVP - Windows XP Media Center
Microsoft Associate Expert
Expert Zone - http://www.microsoft.com/windowsxp/expertzone

"Michael Linster" <mi...@mikelinster.com> wrote in message
news:8b0b1f2.03090...@posting.google.com...

0 new messages