本文介绍了401未经授权的错误,在此处添加了我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我上传文件时出现错误.这是我的代码&bill是文档库的名称.
when i upload the file getting error.this is my code &bill is a document library name.
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);
}
}
推荐答案
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());
}
}
谢谢和问候
sarva
thanks and Regards
sarva
这篇关于401未经授权的错误,在此处添加了我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!