简体   繁体   English

System.IO.FileNotFoundException:无法找到上载的文件

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

We have 2 web servers providing load balance service. 我们有2台Web服务器提供负载平衡服务。 Each IIS host has a temporary folder D:\\Website\\PTSMAM\\UploadFolder\\ to placed uploaded file. 每个IIS主机都有一个临时文件夹D:\\ Website \\ PTSMAM \\ UploadFolder \\,用于放置上载的文件。 When user submit to upload a file, it will upload file to UploadFolder, then move the uploaded file to a permanent storage. 当用户提交上传文件时,它将上传文件到UploadFolder,然后将上传的文件移动到永久存储中。 It happened that if user's request is direct to Server1, it got the following exception 如果用户的请求直接发送到Server1,则会发生以下异常

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

But the file "D:\\Website\\PTSMAM\\UploadFolder\\G201323200010081.txt" exist in its folder. 但是文件“ D:\\ Website \\ PTSMAM \\ UploadFolder \\ G201323200010081.txt”存在于其文件夹中。

If i do the upload job in Server2, it works fine. 如果我在Server2中执行上载作业,则工作正常。 Why is that? 这是为什么? Is it possible that Server1:D:\\Website\\PTSMAM\\UploadFolder and Server2:D:\\Website\\PTSMAM\\UploadFolder may differ? Server1:D:\\ Website \\ PTSMAM \\ UploadFolder和Server2:D:\\ Website \\ PTSMAM \\ UploadFolder是否可能不同?

Following is the source code. 以下是源代码。 I think the exception is induced by System.IO.File.Move(fileFrom, fileDest); 我认为异常是由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;
        }

    }

Look at the permissions of the folder. 查看文件夹的权限。 Does the IIS application pool user has access to it (read/write)? IIS应用程序池用户是否可以访问(读取/写入)? If you can, try login as the application pool user and the look at the folder. 如果可以,请尝试以应用程序池用户身份登录并查看该文件夹。

Assuming the file exists and you checked that it does exists. 假设文件存在并且您检查它确实存在。

  1. Right click the folder D:\\Website\\PTSMAM\\UploadFolder 右键单击文件夹D:\\Website\\PTSMAM\\UploadFolder
  2. Click on properties and then Security Tab. 单击属性,然后单击安全性选项卡。
  3. The Click on edit button 点击编辑按钮
  4. Then Click on add button 然后点击添加按钮
  5. Then add user IIS_IUSRS (checkbox full control should be checked) and then click OK 然后添加用户IIS_IUSRS (应选中复选框完全控制),然后单击“确定”。

The problem should be fixed now. 现在应该解决此问题。

Note Giving full control is not recommended, so you have to decide on what permission needed, if you have security concerns. 注意 :不建议完全控制,因此,如果出于安全考虑,必须决定需要什么权限。

Other issue can be since you are using different drive ( D:\\Website\\PTSMAM\\UploadFolder ) the code string TempDir = Server.MapPath("/") + @"UploadFolder\\"; 其他问题可能是因为您使用的是其他驱动器( D:\\Website\\PTSMAM\\UploadFolder ),代码string TempDir = Server.MapPath("/") + @"UploadFolder\\"; will not find correct path 将找不到正确的路径

Server.MapPath will start finding from the folder which is used to host the web application. Server.MapPath将开始从用于托管Web应用程序的文件夹中查找。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.IO.FileNotFoundException: '系统找不到指定的文件(使用 SetWallpaperAsync) - System.IO.FileNotFoundException: 'The system cannot find the file specified (with SetWallpaperAsync) System.IO.FileNotFoundException:找不到文件 - System.IO.FileNotFoundException : Could not find file System.IO.FileNotFoundException:无法加载文件或程序集。 系统找不到文件SharpSvn.dll - System.IO.FileNotFoundException: Could not load file or assembly. The system cannot find the file SharpSvn.dll System.IO.FileNotFoundException:系统找不到指定的文件。 在IIS 7中(Web服务项目) - System.IO.FileNotFoundException: The system cannot find the file specified. in IIS 7 (web service project) 异常:system.IO.FileNotFoundException:找不到程序集或文件 Mysql.Data - Exception: system.IO.FileNotFoundException: cannot find assembly or file Mysql.Data Xamarin Android System.IO.FileNotFoundException:找不到文件异常 - Xamarin Android System.IO.FileNotFoundException: Could not find file Exception System.IO.FileNotFoundException: '找不到文件 Xamarin Forms - System.IO.FileNotFoundException: 'Could not find file Xamarin Forms 控制台应用程序:System.IO.FileNotFoundException:找不到文件 - Console Application: System.IO.FileNotFoundException: Could not find file System.IO.FileNotFoundException:找不到文件 - System.IO.FileNotFoundException: Can't Find File 'System.IO.FileNotFoundException' - 'System.IO.FileNotFoundException'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM