如何克服 Windows 2012 R2 服务器上的 405 后错误

Posted

技术标签:

【中文标题】如何克服 Windows 2012 R2 服务器上的 405 后错误【英文标题】:How to overcome a post 405 error on windows 2012 R2 server 【发布时间】:2021-02-22 07:17:55 【问题描述】:

我有一个小型测试应用程序来记录相机并将文件发送到我服务器上的目录。 主要文件如下:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://cdn.WebRTC-Experiment.com/RecordRTC.js"></script>
        <style>
            video 
              max-width: 100%;
              border: 5px solid yellow;
              border-radius: 9px;
            
            body 
              background: black;
            
            h1 
              color: yellow;
            
        </style>
    </head>

    <body>
        <h1 id="header">RecordRTC Upload to php</h1>
        <video id="your-video-id" controls="" autoplay=""></video>

        <script type="text/javascript">
            // capture camera and/or microphone
            navigator.mediaDevices.getUserMedia( video: true, audio: true ).then(function(camera) 

                // preview camera during recording
                document.getElementById('your-video-id').muted = true;
                document.getElementById('your-video-id').srcObject = camera;

                // recording configuration/hints/parameters
                var recordingHints = 
                    type: 'video'
                ;

                // initiating the recorder
                var recorder = RecordRTC(camera, recordingHints);

                // starting recording here
                recorder.startRecording();

                // auto stop recording after 5 seconds
                var milliSeconds = 5 * 1000;
                setTimeout(function() 

                    // stop recording
                    recorder.stopRecording(function() 
                        
                        // get recorded blob
                        var blob = recorder.getBlob();

                        // generating a random file name
                        var fileName = getFileName('webm');

                        // we need to upload "File" --- not "Blob"
                        var fileObject = new File([blob], fileName, 
                            type: 'video/webm'
                        );

                        uploadToPHPServer(fileObject, function(response, fileDownloadURL) 
                            if(response !== 'ended') 
                                document.getElementById('header').innerHTML = response; // upload progress
                                return;
                            

                            document.getElementById('header').innerHTML = '<a href="' + fileDownloadURL + '" target="_blank">' + fileDownloadURL + '</a>';

                            alert('Successfully uploaded recorded blob.');

                            // preview uploaded file
                            document.getElementById('your-video-id').src = fileDownloadURL;

                            // open uploaded file in a new tab
                            window.open(fileDownloadURL);
                        );

                        // release camera
                        document.getElementById('your-video-id').srcObject = null;
                        camera.getTracks().forEach(function(track) 
                            track.stop();
                        );

                    );

                , milliSeconds);
            );

            function uploadToPHPServer(blob, callback) 
                // create FormData
                var formData = new FormData();
                formData.append('video-filename', blob.name);
                console.log("blob.name:");
                console.log(blob.name);
                formData.append('video-blob', blob);
                callback('Uploading recorded-file to server.');
                makeXMLHttpRequest('https://xxx/yyy/', formData, function(progress) 
                    if (progress !== 'upload-ended') 
                        callback(progress);
                        return;
                    
                    var initialURL = 'https://xxx/yyy/' + blob.name;
                    callback('ended', initialURL);
                );
            

            function makeXMLHttpRequest(url, data, callback) 
                var request = new XMLHttpRequest();
                request.onreadystatechange = function() 
                    if (request.readyState == 4 && request.status == 200) 
                        if (request.responseText === 'success') 
                            callback('upload-ended');
                            return;
                        
                        alert(request.responseText);
                        return;
                    
                ;
                request.upload.onloadstart = function() 
                    callback('PHP upload started...');
                ;
                request.upload.onprogress = function(event) 
                    callback('PHP upload Progress ' + Math.round(event.loaded / event.total * 100) + "%");
                ;
                request.upload.onload = function() 
                    callback('progress-about-to-end');
                ;
                request.upload.onload = function() 
                    callback('PHP upload ended. Getting file URL.');
                ;
                request.upload.onerror = function(error) 
                    callback('PHP upload failed.');
                ;
                request.upload.onabort = function(error) 
                    callback('PHP upload aborted.');
                ;
                request.open('POST', url);
                request.send(data);
            

            // this function is used to generate random file name
            function getFileName(fileExtension) 
                var d = new Date();
                var year = d.getUTCFullYear();
                var month = d.getUTCMonth();
                var date = d.getUTCDate();
                return 'RecordRTC-' + year + month + date + '-' + getRandomString() + '.' + fileExtension;
            

            function getRandomString() 
                if (window.crypto && window.crypto.getRandomValues && navigator.userAgent.indexOf('Safari') === -1) 
                    var a = window.crypto.getRandomValues(new Uint32Array(3)),
                        token = '';
                    for (var i = 0, l = a.length; i < l; i++) 
                        token += a[i].toString(36);
                    
                    return token;
                 else 
                    return (Math.random() * new Date().getTime()).toString(36).replace(/\./g, '');
                
            
        </script>
    </body>
</html>

在存储文件的位置我有以下文件

<?php


// path to ~/tmp directory
$tempName = $_FILES['video-blob']['tmp_name'];


// move file from ~/tmp to "uploads" directory
if (!move_uploaded_file($tempName, $filePath)) 
    // failure report
    echo getcwd();
    echo " | ";
    echo 'Problem saving file: '.$tempName .' to ' .$filePath .' Not uploaded because of error #'.$_FILES['video-blob']['error'];
    
    if (!is_writable($filePath)) 
        echo " | ";
        echo "dir not writable or existing";
      
    
    die();


// success report
echo 'success';
?>

当我在本地服务器上运行它时,它工作正常。但是当我将它上传到我的 windows 2012 R2 服务器时,我得到了错误

POST https://xxx/yyy/ 405(方法不允许)

我尝试在 ISS 中使用处理程序映射并禁用 WebDAV,但没有运气。 由于它适用于 localhost 但不适用于 windows 服务器,我认为它一定与 IIS 设置有关,但无法找出是什么。

感谢任何帮助。

【问题讨论】:

您确定是脚本故障而不是服务器故障吗?您是否尝试过运行像 echo 'Hello World!'; 这样的简单脚本? 为什么您的请求会发送到https://xxx/yyy/,而不是可能驻留在该文件夹中的实际 PHP 文件? https://xxx/yyy/yourscript.php 【参考方案1】:

最后我自己发现了错误。

web.config 文件中的设置对于 FastCgiModule/StaticFileModules 不正确。

【讨论】:

以上是关于如何克服 Windows 2012 R2 服务器上的 405 后错误的主要内容,如果未能解决你的问题,请参考以下文章