首页 > Web开发 > 详细

ajax input file 提交文件

时间:2016-08-11 22:43:16      阅读:290      评论:0      收藏:0      [点我收藏+]

<!DOCTYPE html>

<html xmlns="技术分享http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    
    <title>Html5 Ajax 上传文件</title>
    <script type="text/javascript">
        function UpladFile() {
            var fileObj = document.getElementById("file").files[0]; // js 获取文件对象
            var FileController = "WebForm1.aspx";                    // 接收上传文件的后台地址            
            // FormData 对象
            var form = new FormData();
            form.append("author", "hooyes");                      // 可以增加表单数据
            form.append("file", fileObj);                           // 文件对象
            // XMLHttpRequest 对象
            var xhr = new XMLHttpRequest();
            xhr.open("post", FileController, true);
            xhr.onload = function () {
               // alert("上传完成!");
            };    
            xhr.send(form);
        }
    </script>

</head>

<body>
    <form id="form1" runat="server">
    <div>
        <input type="file" id="file" name="myfile"/>
         <input type="button" onclick="UpladFile()" value="上传" />
    </div>
    </form>
</body>
</html>
 
 
C#后台
    var flist = Request.Files;

            var form = Request.Form;

            for (int i = 0; i < flist.Count; i++)
            {

                string FilePath = "E:技术分享\\hooyes\\Files\\";

                var c = flist[i];

                FilePath = Path.Combine(FilePath, c.FileName);

                c.SaveAs(FilePath);

            } 

ajax input file 提交文件

原文:http://www.cnblogs.com/gavin0517/p/5762886.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!