【问题标题】:System.IO.FileNotFoundException : Cannot able to find the uploaded fileSystem.IO.FileNotFoundException:找不到上传的文件
【发布时间】:2017-05-25 10:26:12
【问题描述】:

我们有 2 台 Web 服务器提供负载平衡服务。每个 IIS 主机都有一个临时文件夹 D:\Website\PTSMAM\UploadFolder\ 用于放置上传的文件。当用户提交上传文件时,它会将文件上传到 UploadFolder,然后将上传的文件移动到永久存储中。碰巧,如果用户的请求是直接到Server1,它得到了以下异常

System.IO.FileNotFoundException:找不到文件 'D:\Website\PTSMAM\UploadFolder\G201323200010081.txt'。文件名: 'D:\Website\PTSMAM\UploadFolder\G201323200010081.txt' 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.File.Move(String sourceFileName, String destFileName)
在 PTS_MAM3.Web.DataSource.WSARCHIVE.fileMove(ClassUploadFileClass x, 字符串大小写)在 E:\PTSMAM_Source\PTS_MAM\PTS_MAM3.Web\DataSource\WSARCHIVE.asmx.cs:line 第1655章

但文件夹中存在“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;
        }

    }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    查看文件夹的权限。 IIS 应用程序池用户是否有权访问它(读/写)?如果可以,请尝试以应用程序池用户身份登录并查看文件夹。

    【讨论】:

    • 我用应用程序池用户切换登录。它可以通过读/写访问UploadFolder。
    【解决方案2】:

    假设文件存在并且您检查它确实存在。

    1. 右键文件夹D:\Website\PTSMAM\UploadFolder
    2. 单击属性,然后单击安全选项卡。
    3. 点击编辑按钮
    4. 然后点击添加按钮
    5. 然后添加用户IIS_IUSRS(应勾选复选框完全控制)然后点击确定

    问题现在应该解决了。

    注意不建议完全控制,因此如果您有安全问题,您必须决定需要什么权限。

    其他问题可能是因为您使用不同的驱动器 (D:\Website\PTSMAM\UploadFolder),代码 string TempDir = Server.MapPath("/") + @"UploadFolder\"; 将找不到正确的路径

    Server.MapPath 将开始从用于托管 Web 应用程序的文件夹中查找。

    【讨论】:

    • 感谢@GauravKP,我转储了 TempDir,它是一个正确的路径 *D:\Website\PTSMAM\UploadFolder*。我还添加了访问UploadFolder的IIS_IUSRS权限,还是不行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2022-12-01
    • 2019-07-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    相关资源
    最近更新 更多