Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i upload the file getting error.this is my code &bill is a document library name.
C#
protected void btnUpload_Click(object sender, EventArgs e)
        {
            SPSite mySite = SPContext.Current.Site;
            SPWeb myWeb = mySite.OpenWeb();
            UploadDocument(myWeb);  
        }
        public void UploadDocument(SPWeb site)
        {
            if (FileUpload1.HasFile)
            {
                SPFolder folder = site.GetFolder("Bill");
                SPFileCollection files = folder.Files;
                Stream fStream = FileUpload1.PostedFile.InputStream; //path of the file to upload
                byte[] contents = new byte[fStream.Length];
                fStream.Position = 0;
                fStream.Read(contents, 0, (int)fStream.Length);
                fStream.Close();
                string Filename = FileUpload1.FileName;
                string URL = SPContext.Current.Site.Url + "/Bill/" + Filename;
                SPFile currentFile = files.Add(URL, contents);
            }
        }
Posted
Updated 1-Aug-12 2:00am
v3
Comments
Repost of -
http://www.codeproject.com/Questions/432454/401-UNAUTHORIZED-sharepoint
Please delete one.

1 solution

try this code : you need Specify Username and password and port number
C#
Form1 frm = new Form1();
if (frm.IsConnectionAvailable() == true) {
	Label7.Text = "Conection Available";
	NetworkCredential credential = new NetworkCredential("Username", "Password");
	frm.Upload(Application.StartupPath + "\\file.txt", "ftp://ftp.Sitename.in/urfolder/file.txt", credential);
	success();
} else {
	fail();
}

public bool IsConnectionAvailable()
{
	// Returns True if connection is available
	// Replace www.yoursite.com with a site that
	// is guaranteed to be online - perhaps your
	// corporate site, or microsoft.com
	System.Uri objUrl = new System.Uri("http://www.google.com/");
	// Setup WebRequest
	System.Net.WebRequest objWebReq = null;
	objWebReq = System.Net.WebRequest.Create(objUrl);
	System.Net.WebResponse objResp = null;
	try {
		// Attempt to get response and return True
		objResp = objWebReq.GetResponse();
		objResp.Close();
		objWebReq = null;
		return true;
	} catch (Exception ex) {
		// Error, exit and return False
		fail();
		objResp.Close();
		objWebReq = null;
		return false;
	}
}

public void Upload(string source, string target, NetworkCredential credential)
{
	try {
		FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
		request.Credentials = credential;
		request.KeepAlive = false;
		request.Proxy = null;
		request.Timeout = 10000000;
		request.ReadWriteTimeout = 10000000;
		request.Method = WebRequestMethods.Ftp.UploadFile;
		//FileCopy(source, Application.StartupPath & "\Nirmala.mdb")
		FileStream reader = new FileStream(Application.StartupPath + "\\Nirmala.mdb", FileMode.Open);
		request.UseBinary = true;
		byte[] buffer = new byte[Convert.ToInt32(reader.Length - 1) + 1];
		reader.Read(buffer, 0, buffer.Length);
		reader.Close();
		request.ContentLength = buffer.Length;
		Stream stream = request.GetRequestStream;
		stream.Write(buffer, 0, buffer.Length);
		stream.Close();
		FtpWebResponse response = (FtpWebResponse)request.GetResponse;
		//MessageBox.Show(response.StatusDescription, "File Uploaded")
		response.Close();
		Label8.Text = "Upload Success";
	} catch (Exception ex) {
		Interaction.MsgBox(ex.Message.ToString());
	}
}

thanks and Regards
sarva
 
Share this answer
 
v5
Comments
oliver grace 1-Aug-12 7:32am    
thanks for your reply but i want to upload my document in sharepoint document library.is there any other way..
Sandeep Mewara 1-Aug-12 8:04am    
Always wrap your code with PRE tags.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900