簡體   English   中英

System.IO.FileNotFoundException:無法找到上載的文件

[英]System.IO.FileNotFoundException : Cannot able to find the uploaded file

我們有2台Web服務器提供負載平衡服務。 每個IIS主機都有一個臨時文件夾D:\\ Website \\ PTSMAM \\ UploadFolder \\,用於放置上載的文件。 當用戶提交上傳文件時,它將上傳文件到UploadFolder,然后將上傳的文件移動到永久存儲中。 如果用戶的請求直接發送到Server1,則會發生以下異常

System.IO.FileNotFoundException:找不到文件'D:\\ Website \\ PTSMAM \\ UploadFolder \\ G201323200010081.txt'。 文件名:System.IO .__ Error.WinIOError中的'D:\\ Website \\ PTSMAM \\ UploadFolder \\ G201323200010081.txt'(Int32 errorCode,String mayFullPath)
在System.IO.File.Move(字符串sourceFileName,字符串destFileName)
在E:\\ PTSMAM_Source \\ PTS_MAM \\ PTS_MAM3.Web \\ DataSource \\ WSARCHIVE.asmx.cs:line 1655中的PTS_MAM3.Web.DataSource.WSARCHIVE.fileMove(ClassUploadFileClass x,String scase)

但是文件“ D:\\ Website \\ PTSMAM \\ UploadFolder \\ G201323200010081.txt”存在於其文件夾中。

如果我在Server2中執行上載作業,則工作正常。 這是為什么? Server1:D:\\ Website \\ PTSMAM \\ UploadFolder和Server2:D:\\ Website \\ PTSMAM \\ UploadFolder是否可能不同?

以下是源代碼。 我認為異常是由System.IO.File.Move(fileFrom,fileDest);引起的;

    private bool fileMove(ClassUploadFileClass x, string scase)
    {
        string fileFrom = string.Empty;
        string fileFromV = string.Empty;
        string fileDest = x.strDirectory;
        //string fileDest2 = string.Concat(System.IO.Path.GetDirectoryName(fileDest), @"\", System.IO.Path.GetFileNameWithoutExtension(fileDest), "_M", System.IO.Path.GetExtension(fileDest));
        string fileDest2 = string.Concat(System.IO.Path.GetDirectoryName(fileDest), @"\", System.IO.Path.GetFileNameWithoutExtension(fileDest), "_M.jpg");
        string TempDir = Server.MapPath("/") + @"UploadFolder\";
        //MAM_PTS_DLL.SysConfig obj = new MAM_PTS_DLL.SysConfig();
        //string dirPhoto = obj.sysConfig_Read("/ServerConfig/STO_Config/UPLOAD_FOLDER_PHOTO");
        //string dirDoc = obj.sysConfig_Read("/ServerConfig/STO_Config/UPLOAD_FOLDER_DOC");
        //string dirAudio = obj.sysConfig_Read("/ServerConfig/STO_Config/UPLOAD_FOLDER_AUDIO");

        try
        {
            //MAM_PTS_DLL.Log.AppendTrackingLog("WSARchive/fileMove", MAM_PTS_DLL.Log.TRACKING_LEVEL.INFO, "Path = " + fileDest);// + @"\" + x.strFileId + "." + x.strOExtensionName);
            switch (scase)
            {
                case ("Photo"):
                    fileFrom = string.Concat(TempDir, x.strFileId, ".", x.strOExtensionName);//@"\\10.13.220.2\uploadfolder\Pictures\"
                    //fileFromV = string.Concat(TempDir, x.strFileId, "_M.", x.strOExtensionName);//@"\\10.13.220.2\uploadfolder\Pictures\"
                    fileFromV = string.Concat(TempDir, x.strFileId, "_M.jpg");
                    break;
                case ("Audio"):
                    fileFrom = string.Concat(TempDir, x.strFileId, ".", x.strOExtensionName);//@"\\10.13.220.2\uploadfolder\Audios\"
                    break;
                case ("Doc"):
                    fileFrom = string.Concat(TempDir, x.strFileId, ".", x.strOExtensionName);
                    break;
                case ("LowVideo"):
                    fileFrom = string.Concat(TempDir, x.strFileId, ".", x.strOExtensionName);
                    break;
            }

            string dirPath = System.IO.Path.GetDirectoryName(fileDest);
            if (!System.IO.Directory.Exists(dirPath))
                System.IO.Directory.CreateDirectory(dirPath);
            System.IO.File.Move(fileFrom, fileDest);        // + @"\" + x.strFileId + "." + x.strOExtensionName);

            if (scase == "Photo")
                System.IO.File.Move(fileFromV, fileDest2);

            return true;
        }
        catch (Exception ex)
        {
            MAM_PTS_DLL.Log.AppendTrackingLog("File Move Problem", MAM_PTS_DLL.Log.TRACKING_LEVEL.ERROR, x.strOFileName + ",ex = " + ex.ToString());
            return false;
        }

    }

查看文件夾的權限。 IIS應用程序池用戶是否可以訪問(讀取/寫入)? 如果可以,請嘗試以應用程序池用戶身份登錄並查看該文件夾。

假設文件存在並且您檢查它確實存在。

  1. 右鍵單擊文件夾D:\\Website\\PTSMAM\\UploadFolder
  2. 單擊屬性,然后單擊安全性選項卡。
  3. 點擊編輯按鈕
  4. 然后點擊添加按鈕
  5. 然后添加用戶IIS_IUSRS (應選中復選框完全控制),然后單擊“確定”。

現在應該解決此問題。

注意 :不建議完全控制,因此,如果出於安全考慮,必須決定需要什么權限。

其他問題可能是因為您使用的是其他驅動器( D:\\Website\\PTSMAM\\UploadFolder ),代碼string TempDir = Server.MapPath("/") + @"UploadFolder\\"; 將找不到正確的路徑

Server.MapPath將開始從用於托管Web應用程序的文件夾中查找。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM