<..._vue上传文件到后端">
当前位置:   article > 正文

vue.js上传文件到后端_vue上传文件到后端

vue上传文件到后端

本文实例为大家分享了vue.js异步上传文件的具体代码,供大家参考,具体内容如下:

1、上传文件前端代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5.   <title></title>
  6.   <meta charset="utf-8" />
  7.   <script src="../js/vue.js"></script>
  8.   <script src="../js/vue-resource.js"></script>
  9.   <script src="../asset/js/jquery.js"></script>
  10. </head>
  11. <body>
  12.   <div id="app">
  13.     <input type="file" @change="getFile($event)" /><button @click="upload">上传</button>
  14.     <div>{{ result }}</div>
  15.     <div v-show="uping==1">正在上传中</div>
  16.   </div>
  17. <script>
  18.   new Vue({
  19.     el: '#app',
  20.     data: {
  21.       upath: '',
  22.       result: '',
  23.       uping:0
  24.     },
  25.     methods: {
  26.       upload: function () {
  27.         //console.log(this.upath);
  28.         var zipFormData = new FormData();
  29.         zipFormData.append('filename', this.upath);//filename是键,file是值,就是要传的文件,test.zip是要传的文件名
  30.         let config = { headers: { 'Content-Type': 'multipart/form-data' } };
  31.         this.uping = 1;
  32.         this.$http.post('http://localhost:42565/home/up', zipFormData,config).then(function (response) {
  33.           console.log(response);
  34.           console.log(response.data);
  35.           console.log(response.bodyText);
  36.            
  37.           var resultobj = response.data;
  38.           this.uping = 0;
  39.           this.result = resultobj.msg;
  40.         });
  41.       },
  42.       getFile: function (even) {
  43.         this.upath = event.target.files[0];
  44.       },
  45.     }
  46.   });
  47. </script>
  48. </body>
  49. </html>

2、后端处理代码如下asp.net mvc的:

  1. public ActionResult Up()
  2.     {
  3.       string msg = string.Empty;
  4.       string error = string.Empty;
  5.       string result = string.Empty;
  6.       string filePath = string.Empty;
  7.       string fileNewName = string.Empty;
  8.       var files = Request.Files;
  9.       if (files.Count > 0)
  10.       {
  11.         //设置文件名
  12.         fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + System.IO.Path.GetFileName(files[0].FileName);
  13.         //保存文件
  14.         files[0].SaveAs(Server.MapPath("~/Uploads/" + fileNewName));
  15.         Thread.Sleep(10 * 1000);
  16.       }
  17.       return Json(new { msg = "上传成功", newfilename = fileNewName }, JsonRequestBehavior.AllowGet);
  18.     }

以上就是本文的全部内容,希望对大家的学习有所帮助。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/171237
推荐阅读
相关标签
  

闽ICP备14008679号