首页 > 代码库 > 【cors跨域】H5 js跨域上传文件,C#服务端接收
【cors跨域】H5 js跨域上传文件,C#服务端接收
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="http://www.mamicode.com/上传" 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="http://www.mamicode.com/*" /> <add name="Access-Control-Allow-Headers" value="http://www.mamicode.com/X-Requested-With,Content-Type" /> <add name="Access-Control-Allow-Methods" value="http://www.mamicode.com/GET, POST, PUT, DELETE, OPTIONS" /> </customHeaders> </httpProtocol> </system.webServer>
【cors跨域】H5 js跨域上传文件,C#服务端接收
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。