目前有一个需求,需要通过 Winform界面上传文件到服务器,写了如下的方法,方法调用:
UploadFile(@"\\10.2.11.23\d$","1.txt") ; 但是用这种方法上传不了,提示路径拒绝访问,请问大家指教(我知道设置IIS和FTP是可行的,但是不想使用这种方法,希望在不改变服务器配置的情况下,实现和传功能) private void UpLoadFile(string fileNamePath, string urlPath)
{
string newFileName = fileNamePath.Substring(fileNamePath.LastIndexOf(@"\") + 1); if (urlPath.EndsWith(@"\") == false) urlPath = urlPath + @"\"; urlPath = urlPath + newFileName; WebClient myWebClient = new WebClient();
NetworkCredential cread = new NetworkCredential("Administrator", "XXXXX", "Domain");
myWebClient.Credentials = cread;
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(urlPath );
// postStream.m
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
MessageBox.Show("UploadFile is OK...");
}
else
{
MessageBox.Show("file cannot write!");
} postStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error" );
}
}
UploadFile(@"\\10.2.11.23\d$","1.txt") ; 但是用这种方法上传不了,提示路径拒绝访问,请问大家指教(我知道设置IIS和FTP是可行的,但是不想使用这种方法,希望在不改变服务器配置的情况下,实现和传功能) private void UpLoadFile(string fileNamePath, string urlPath)
{
string newFileName = fileNamePath.Substring(fileNamePath.LastIndexOf(@"\") + 1); if (urlPath.EndsWith(@"\") == false) urlPath = urlPath + @"\"; urlPath = urlPath + newFileName; WebClient myWebClient = new WebClient();
NetworkCredential cread = new NetworkCredential("Administrator", "XXXXX", "Domain");
myWebClient.Credentials = cread;
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(urlPath );
// postStream.m
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
MessageBox.Show("UploadFile is OK...");
}
else
{
MessageBox.Show("file cannot write!");
} postStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error" );
}
}
需要密码的话
http://jackyxfl.blog.163.com/blog/static/164134150201110352115704/
我是想测试一下,我用的是Administrator , 肯定是有写的权限的
FTP 是可以的,这样做我会的, 因为 有webclinet类,我是想看看用这个类,或者 API 能否实现?