【cors跨域】H5 js跨域上传文件,C#服务端接收

时间:2021-11-14 05:12:44

H5原生js文件上传,带进度。

js代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form action="">
        <div>文件选择</div>
        <input type="file" accept="audio/*" capture="microphone" id="fileToUpload" onchange="fileSelected()"> 
        <div>
            <div id="fileName"></div>
            <div id="fileSize"></div>
            <div id="fileType"></div>
        </div>
        <div id="progressNumber"></div>
        <div><input type="button" value="上传" onclick="uploadFile()" /></div>
    </form>
</body>
</html>
<script>
    function fileSelected() {
        var file = document.getElementById('fileToUpload').files[0];
        if (file) {
            var fileSize = 0;
            if (file.size > 1024 * 1024)
                fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
            else
                fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';

            document.getElementById('fileName').innerHTML = 'Name: ' + file.name;
            document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize;
            document.getElementById('fileType').innerHTML = 'Type: ' + file.type;
        }
    }

    function uploadFile() {
        var fd = new FormData();
        fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
        var xhr = new XMLHttpRequest();
        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false);
        xhr.addEventListener("error", uploadFailed, false);
        xhr.addEventListener("abort", uploadCanceled, false);
        xhr.open("POST", "http://192.168.137.20:8012/Handler1.ashx");
        xhr.send(fd);
    }

    function uploadProgress(evt) {
        if (evt.lengthComputable) {
            var percentComplete = Math.round(evt.loaded * 100 / evt.total);
            document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
        }
        else {
            document.getElementById('progressNumber').innerHTML = 'unable to compute';
        }
    }

    function uploadComplete(evt) {
        /* This event is raised when the server send back a response */
        //alert(evt.target.responseText);
    }

    function uploadFailed(evt) {
        alert("uploadFailed:" + JSON.stringify(evt));
    }

    function uploadCanceled(evt) {
        alert("uploadCanceled:" + JSON.stringify(evt));
    }
</script>

 C#部分

public void ProcessRequest(HttpContext context)
        {
            HttpFileCollection files =context.Request.Files;
            if (files != null && files.Count > 0)
            {
                string filename = files[0].FileName;
                var filesave = "E:\\file\\" + filename;
                files[0].SaveAs(filesave);
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

 C#配置cors允许请求头(通过webconfig配置),尝试过写入global的方式get可以,post不行(进不了global部分代码),未找到解决方案

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="X-Requested-With,Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>