我有一个网络应用程序。我们可以通过两种方式将文件上传到某个位置。
我们有一个Fileupload控件,用户可以从中将文件上传到一个位置(例如:\ testmachine \ share),该位置将分享给每个人。
我们还有一个自动化作业,可自动执行此操作。因此,当我们将文件放在特定的文件位置(例如:\ testmachine2 \ share2)或FTP时,作业会将此文件下载到共享位置(例如:\ testmachine \ share)。
但有时,用户无法使用第一个方案上传文件。整个问题发生在生产服务器上,我没有任何访问权限。 正在研究这个问题时,我得到了痕迹。这就是追踪。
Access to the path **\\testmachine\share\test.zip** is denied.,mscorlib, at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at System.Web.HttpPostedFile.SaveAs(String filename)
at System.Web.UI.WebControls.FileUpload.SaveAs(String filename)
现在我尝试在本地计算机上重现相同的行为,并且能够重现这种情况。
以下是代码:
public static IList<DownloadInfo> Download(XmlDataDocument xDoc)
{
List<DownloadInfo> downloadedList = new List<DownloadInfo>();
WebClient request = new WebClient();
string user = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("user").Value;
string password = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("password").Value;
//domain is not used Curretly. This might be useful when UNC is implemented in authenticated mode.
string domain = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("domain").Value;
request.Credentials = new NetworkCredential(user, password);
FileStream file = null;
try
{
string serverType = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("type").Value;
string serverPath = xDoc.SelectSingleNode("configurationSettings/fileSourceServer").Attributes.GetNamedItem("path").Value;
if (serverType.Equals("ftp"))
{
if (!serverPath.ToLower().StartsWith("ftp://"))
{
serverPath = "ftp://" + serverPath.Trim();
}
else
{
serverPath = serverPath.Trim();
}
FtpWebRequest fwr = (FtpWebRequest)FtpWebRequest.Create(new Uri(serverPath));
fwr.Credentials = request.Credentials;
fwr.Method = WebRequestMethods.Ftp.ListDirectory;
StreamReader sr = new StreamReader(fwr.GetResponse().GetResponseStream());
string str = sr.ReadLine();
while (str != null)
{
if (str.ToUpper().EndsWith(".ZIP"))
{
DownloadInfo dwnInfo = new DownloadInfo();
dwnInfo.SourceServerZipFilePath = str;
downloadedList.Add(dwnInfo);
}
str = sr.ReadLine();
}
// construct the server path, if file location is provided instead directory location.
if (downloadedList.Count == 1)
{
if (serverPath.EndsWith(downloadedList[0].SourceServerZipFilePath))
{
string[] delimiter = { downloadedList[0].SourceServerZipFilePath };
serverPath = serverPath.Split(delimiter, StringSplitOptions.RemoveEmptyEntries)[0];
}
}
sr.Close();
sr = null;
fwr = null;
}
else if (serverType.Equals("file"))
{
//TODO in authenticated mode.
if (!serverPath.ToLower().StartsWith(@"\\"))
{
serverPath = Path.GetFullPath(@"\\" + serverPath.Trim());
}
else
{
serverPath = Path.GetFullPath(serverPath.Trim());
}
DirectoryInfo dInfo = new DirectoryInfo(serverPath);
FileInfo fInfo = new FileInfo(serverPath);
if (dInfo.Exists)
{
FileInfo[] filelist = dInfo.GetFiles("*.zip");
foreach (FileInfo f in filelist)
{
DownloadInfo dwnInfo = new DownloadInfo();
dwnInfo.SourceServerZipFilePath = f.Name;
downloadedList.Add(dwnInfo);
}
}
else if (fInfo.Exists && fInfo.Extension.ToUpper() == ".ZIP")
{
DownloadInfo dwnInfo = new DownloadInfo();
dwnInfo.SourceServerZipFilePath = fInfo.Name;
downloadedList.Add(dwnInfo);
serverPath = fInfo.DirectoryName;
}
else if (!dInfo.Exists || !fInfo.Exists)
Logger.Error(String.Format("{0} is not accessible. Make sure the folder exists and the machine account where the system agent is installed has access to the folder.", serverPath));
}
if (downloadedList.Count == 0)
Logger.Warn(string.Format("{0} does not have a ZIP file. Make sure the folder contains the ZIP file for each dataset.", serverPath));
//Copy files to destination location (upload folder)
foreach (DownloadInfo dwnInfo in downloadedList)
{
string strFile = dwnInfo.SourceServerZipFilePath;
DateTime time = new DateTime();
time = DateTime.Now;
string date = time.ToString("yyyyMMdd-HHmmss_");
Uri UriPath = new Uri(serverPath + "/" + strFile);
byte[] filedata = request.DownloadData(UriPath);
// create the destination path.
string destPath = xDoc.SelectSingleNode("configurationSettings/fileDestinationServer").Attributes.GetNamedItem("path").Value;
destPath = Path.Combine(destPath.Trim(), date + strFile);
file = File.Create(destPath);
file.Write(filedata, 0, filedata.Length);
file.Close();
file = null;
//changing source server path to full path. Earlier only file name was assigned.
dwnInfo.SourceServerZipFilePath = UriPath.OriginalString;
dwnInfo.DestinationServerZipFilePath = destPath;
//System.Console.WriteLine(strFile + " - Download Complete.");
}
//Extract all the downloded zip files at destination location.
extractFile(downloadedList);
}
catch (Exception ex)
{
Logger.Error(String.Format("Exception occured: {0}", ex.Message), ex);
}
finally
{
if (file != null)
file.Close();
}
return downloadedList;
}
private static void extractFile(IList<DownloadInfo> fileList)
{
ZipUtils zip = new ZipUtils();
foreach (DownloadInfo dwnInfo in fileList)
{
try
{
FileInfo fInfo = new FileInfo(dwnInfo.DestinationServerZipFilePath);
zip.extract(fInfo.FullName, fInfo.FullName.Replace(fInfo.Extension, ""));
dwnInfo.DestinationServerUnzipFilePath = fInfo.FullName.Replace(fInfo.Extension, "");
}
catch (Exception ex)
{
Logger.Error(String.Format("Exception occured during extracting {0}", dwnInfo.SourceServerZipFilePath), ex);
}
}
}
但是当我重新启动存在共享文件夹的机器时,我不再遇到问题。
任何人都可以帮我解决这个问题,也很有可能重现这个问题。
谢谢,
维奈。