.NET中的FileUpload控件的使用-原生JS(二)

本篇使用原生JS进行数据传输,使用FileUpload控件上传文件,适配IE。

HTML

1 <div class="container">
2         <div class="row">
3             <input type="file" formenctype="multipart/form-data" id="files" multiple name="files" /><button class="btn-default" id="UploadButton">点我上传</button>
4         </div>
5     </div>
View Code

 

JS

 1  <script type="text/javascript">
 2         var fileData = new FormData();
 3         fileData.enctype = "multipart/form-data";//给定数据传输格式
 4         window.onload = function () {
 5             document.getElementById("UploadButton").onclick = function () {
 6                 debugger;
 7                 fileData.append("subject", "哈哈哈");
 8                 fileData.append("remark", "12366");
 9                 var files = document.getElementById("files").files;
10                 if (files.length > 0) {
11                     for (var i = 0; i < files.length; i++) {
12                         fileData.append(files[i].name, files[i]);
13                     }
14                 }
15                 //var filedata = json.stringify(data);
16                 SpadesQ({
17                     url: '/IENotes/Create',
18                     method: 'POST',
19                     async: false,
20                    contentType: false,
21                      processData: false,
22                      data: fileData,
23                 success: function (result) {
24                     alert(result);
25                 },
26                 error: function (error) {
27                     alert(error);
28                 }
29             });
30             }
31         }
32   </script>
33 用原生JS封装了一个简易的Ajax
34 /*封装Ajax函数
35  @param {string} opt.type http 连接的方式 包括POST、GET
36  @param {string} opt.url  请求的地址
37  @param {boolean} async  是否异步
38  @param {object} otp.data 请求发出的数据
39  @param {boolean} otp.contentType 传输中的数据格式
40  @param {boolean} otp.processData 是否将数据序列化
41  @param {function} success 请求成功后的回调
42  @param {function} error 出错后的回调
43 */
44 function SpadesQ(opt) {
45     debugger;
46     opt = opt || {};
47     opt.method = (opt.method || 'GET').toUpperCase();
48     opt.url = opt.url || '';
49     opt.async = opt.async || false;
50     opt.contentType = opt.contentType || false; //contentType设为false,不修对象改格式
51     opt.processData = opt.processData || false;//不对数据对象做序列化操作
52     opt.data = opt.data ||{};
53     opt.success = opt.success || function () { };
54     opt.error = opt.error || function () { };
55     var xmlHttp = null;
56     if (XMLHttpRequest) {
57         xmlHttp = new XMLHttpRequest;
58     } else {
59         xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
60     }
61     if (opt.method.toUpperCase() === 'POST') {
62         xmlHttp.open(opt.method, opt.url, opt.async);//因为此处是为了完整的将FormData对象传送至到action中,所以没有对数据做任何改变,有需要对数据格式进行处理的另当别论。
63         //xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charest=utf-8');
64         xmlHttp.send(opt.data);
65     } else if(opt.method.toUpperCase()==='GET') {
66         xmlHttp.open(opt.method, opt.url + '?' + opt.data, opt.async);
67         xmlHttp.send();
68     }
69     xmlHttp.onreadystatechange = function () {
70         if (xmlHttp.status == 200 && xmlHttp.readyState == 4) {
71             opt.success(xmlHttp.responseText);
72         } else {
73             opt.error(xmlHttp.responseText);
74         }
75     };
76 }
View Code

 

后台逻辑

 1 public static bool CommonFile(){
 2  var  files = Request.Files.AllKeys.Distinct(); //将传输过来的files进行去重
 3                 if (files != null)
 4                 {
 5                     foreach (string each in files)
 6                     {
 7                         HttpPostedFileBase file = Request.Files[each] as HttpPostedFileBase; //注意,这里有一个对象类型转换的过程,需要将传过来的对象转换成HttpPostedFileBase对象
 8                         if (file != null)
 9                         {
10                             var InputFileName = Path.GetFileName(file.FileName);
11                             string str4 = AppDomain.CurrentDomain.BaseDirectory;    //获取基目录,它由程序集冲突解决程序用来探测程序集。
12                             var ServerSavePath = Path.Combine(Server.MapPath("~/Uploads/") );
13                             if (!Directory.Exists(ServerSavePath))//检查路径是否存在
14                             {
15                                 Directory.CreateDirectory(ServerSavePath);
16                             }
17                             var SavePath = Path.Combine(ServerSavePath ,InputFileName);
18                             file.SaveAs(SavePath);
19                         }
20                     }
21                     return true;
22                 }
23                 else
24                 {
25                     return false;
26                 }
27 }
View Code

 

点我

感谢您的观看,您的👍是对我最大的支持!

 

posted on 2019-01-23 09:45  大肖  阅读(206)  评论(0编辑  收藏  举报

导航