1

私はウェブアプリケーションを持っています。そのため、2 つの方法でファイルを場所にアップロードできます。

  1. 全員に共有される場所 (例:\testmachine\share) にユーザーがファイルをアップロードできる Fileupload コントロールがあります。

  2. これを自動的に行う自動ジョブもあります。そのため、ファイルを特定のファイルの場所 (例: \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);
        }
      }
    }

しかし、共有フォルダーが存在するマシンを再起動すると、問題は発生しなくなりました。

これを修正するために私を助けてください。また、問題を再現することもできます。

ありがとう、

ビナイ。

4

0 に答える 0