WebClient 上传文件到网页 [英] WebClient Upload file to web
问题描述
我想知道我是否正确编码.要将文件手动上传到我的工作区服务器,我必须使用登录 ID 和密码.在下面的代码中,我是否还应该包括我的登录 ID 和密码?
I would like to know if I code it correctly. To upload file manually to my workplace server, I have to use Login ID and Password. With the clode below, should I include my loginID and Password as well?
public void SaveLogsToWeb(string logFileName)
{
WebClient webClient = new WebClient();
string webAddress = null;
try
{
webAddress = @"http://myCompany/ShareDoc/";
webClient.Credentials = CredentialCache.DefaultCredentials;
WebRequest serverRequest = WebRequest.Create(webAddress);
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();
webClient.UploadFile(webAddress + logFileName, "PUT", logFileName);
webClient.Dispose();
webClient = null;
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
当我运行它时,异常抛出(401) Unauthorized"
When I run it, exception throws "(401) Unauthorized"
谢谢.
推荐答案
永远不要在代码文件中包含用户/密码信息.之所以抛出 401,是因为 Internet 用户及其运行的应用程序池对您尝试写入的目录没有写入权限.
You should never include user/password information in a code file. The reason this is throwing up a 401 is because the internet user and the application pool it's running under don't have write permissions to the directory you're attempting to write to.
右键单击目录并添加/ASPNET 和/Network Service 作为具有写入权限的用户.这应该可以解决问题.确保隔离目录.
Right-click on the directory and add /ASPNET and /Network Service as users with write permission. This should clear up the problem. Make sure you isolate the directory.
这里有一篇很好的 msdn 文章:http://support.microsoft.com/kb/815153
Here's a good msdn article on it: http://support.microsoft.com/kb/815153
这篇关于WebClient 上传文件到网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!