Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have created asp.net web application to put document into sharepoint.
when i upload my document i am getting this error.how to solve this.

using ASP.NET I have created application.when i upload any file in that that should be stored into sharepoint document library.


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:25am
v2
Comments
efkah 1-Aug-12 7:26am    
Who uploads the Document? Workflow or TimerJob? User? Does he have Rights on the List? Did you try RunWithElevatedPrivileges? How do you upload the Document?
oliver grace 1-Aug-12 7:37am    
using ASP.NET I have created application.when i upload any file in that that should be stored into sharepoint document library.


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);
}
}
Sebastian T Xavier 1-Aug-12 8:24am    
Use improve question link to update your question
pradiprenushe 1-Aug-12 8:34am    
What error?
Repost of -
http://www.codeproject.com/Questions/432594/401-unauthorized-error-added-my-code-here
Please delete one.

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