Ajax检测本地文件存在失败,无法完成文件移动求助
First off, the reason your Ajax calls are returning "file doesn't exist" is because browsers (including IE) block Ajax requests to local file system paths like C:\Users\romama\... due to security restrictions. Even though the files are there, the browser won't let your JavaScript make HEAD requests to local files via Ajax—this violates the same-origin policy and is a deliberate security guardrail.
But here's the kicker: you don't need Ajax at all here! You already have direct access to the files via the FileSystemObject—you're already iterating over the folder's files, so you know they exist. Skip the unnecessary Ajax check entirely and move the files directly using FSO's built-in methods.
Modified Code to Move Files Directly
Here's how to adjust your code to get the file move working without relying on Ajax:
moveErrorFiles('C:/Users/romama/Desktop/HMI Versions/V 0.5/web/css'); function moveErrorFiles(fileDir){ var fileSysObj, folder, fileCounter, currentFile; fileSysObj = new ActiveXObject("Scripting.FileSystemObject"); folder = fileSysObj.GetFolder(fileDir); // Define your target directory here (update this to your actual target path!) var targetDir = "C:/Users/romama/Desktop/ErrorFiles"; // Create target folder if it doesn't exist if (!fileSysObj.FolderExists(targetDir)) { fileSysObj.CreateFolder(targetDir); } fileCounter = new Enumerator(folder.files); for (; !fileCounter.atEnd(); fileCounter.moveNext()) { currentFile = fileCounter.item(); try { // Use FSO's MoveFile method to transfer the file fileSysObj.MoveFile(currentFile.Path, targetDir + "/"); console.log(currentFile.Name + " moved successfully. Size: " + currentFile.Size); } catch (e) { console.log("Failed to move " + currentFile.Name + ": " + e.message); } } }
Key Tips:
- Target Path: Double-check the
targetDirvalue to make sure it points to the folder you want to move files into. - Permissions: IE might prompt you to allow ActiveX access—you'll need to grant this for the script to modify your file system. Also, ensure your user account has write permissions for both the source and target folders.
- ActiveX Limitation: Keep in mind this code only works in Internet Explorer, since other modern browsers don't support the
Scripting.FileSystemObjectActiveX control.
By ditching the Ajax detour and using the direct file system access you already have, you can get the file move functionality working as intended.
内容的提问来源于stack exchange,提问作者Maxim Roman

