none
UPload to WSS Document Library RRS feed

  • Question

  • I have function named upload file that helps me to download file to the remote server WSS Document Library.This works fine when i use a client machine having the Windows server 2008 and Sharepoint Installed in it.But when i tried to upload using the client machine having Windows Xp  code

    WebResponse ores = req.GetResponse(); returns Error Code:504 gateway time-out.

    For Reference:

    http://blogs.msdn.com/b/rohitpuri/archive/2007/04/10/upload-download-file-to-from-wss-document-library-using-dav.aspx

    The Code is attached for your reference.

    public static string UploadFile(string destUrl, string sourcePath)

    {

    try

    {

    Uri destUri = new Uri(destUrl);

    FileStream inStream = File.OpenRead(sourcePath);

    WebRequest req = WebRequest.Create(destUri);


    req.Method = "PUT" ;

    req.Headers.Add("Overwrite", "F");

    req.Timeout = System.Threading.Timeout.Infinite;

    //req.Credentials = CredentialCache.DefaultCredentials;
    req.Credentials = new NetworkCredential("username", "password", "domain");


    Stream outStream = req.GetRequestStream();

    string status = CopyStream(inStream, outStream);

    if (status == "success")


    {

    outStream.Close();

    WebResponse ores = req.GetResponse();

    return "success";


    }

    else

    {

    return status;

    }

    }

    catch (WebException we)

    {

    return we.Message;

    }

    catch (System.Exception ee)

    {

    return ee.Message;

    }

    }
            private static string CopyStream(Stream inStream, Stream outStream)


    {

    try

    {

    byte[] buffer = new byte[1024];

    for (; ; )

    {

    int numBytesRead = inStream.Read(buffer, 0, buffer.Length);

    if (numBytesRead <= 0)

    break;

    outStream.Write(buffer, 0, numBytesRead);


    }

    inStream.Close();

    return "success";


    }

    catch (System.Exception ee)

    {

    return ee.Message;

    }

    }

    Thursday, June 9, 2011 6:54 AM

All replies

  • So you actually want to save a file towards a document library ?

     

    if so

     

    use this

     

    String fileToUpload = @"C:\YourFile.txt";
    String sharePointSite = "http://yoursite.com/sites/Research/";
    String documentLibraryName = "Shared Documents";

    using (SPSite oSite = new SPSite(sharePointSite))
    {
    using (SPWeb oWeb = oSite.OpenWeb())
    {
    if (!System.IO.File.Exists(fileToUpload))
    throw new FileNotFoundException("File not found.", fileToUpload);

    SPFolder myLibrary = oWeb.Folders[documentLibraryName];

    // Prepare to upload
    Boolean replaceExistingFiles = true;
    String fileName = System.IO.Path.GetFileName(fileToUpload);
    FileStream fileStream = File.OpenRead(fileToUpload);

    // Upload document
    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

    // Commit
    myLibrary.Update();
    }
    }


    Moderator Note: NEVER propose your own posts as answers. The function is there so that people can propose the good answers of other people. NOT for self-proposing.
    Thursday, June 9, 2011 7:07 AM
  • Propose your own posts as answers again and I will delete your posts.

    Proposing the previous post again after being told in very clear words not to is something I will NOT tolerate.

     

    Moderator


    SP 2010 "FAQ" (mainly useful links): http://wssv4faq.mindsharp.com/default.aspx
    WSS3/MOSS FAQ (FAQ and Links) http://wssv3faq.mindsharp.com/default.aspx
    Both also have links to extensive book lists and to (free) on-line chapters
    Thursday, June 9, 2011 12:34 PM
  • hi Sebastian,

    iam using:Windows Xp.

    Add a dll:Microsoft.SharePoint   to my project

    When i runs the program,getting the following error:

    Could not load file or assembly 'Microsoft.SharePoint.Library, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified

    Thank you,

    Monday, June 13, 2011 10:55 AM