码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-12-15 20:31:02      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:doc   ble   apt   hang   button   eth   str   rip   this   

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>

 

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

标签:doc   ble   apt   hang   button   eth   str   rip   this   

原文地址:http://www.cnblogs.com/zspbolg/p/6184330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!